-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberOfValidWordsForEachPuzzle.java
More file actions
162 lines (142 loc) · 5.48 KB
/
NumberOfValidWordsForEachPuzzle.java
File metadata and controls
162 lines (142 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package leetcode;
import java.util.*;
/**
* NumberOfValidWordsForEachPuzzle
* https://leetcode-cn.com/problems/number-of-valid-words-for-each-puzzle
* 1178. 猜字谜
* https://leetcode-cn.com/problems/number-of-valid-words-for-each-puzzle/solution/er-jin-zhi-ya-suo-er-jin-zhi-qiu-zi-ji-f-1a4p/
*
* @since 2021-02-26
*/
public class NumberOfValidWordsForEachPuzzle {
public static void main(String[] args) {
NumberOfValidWordsForEachPuzzle sol = new NumberOfValidWordsForEachPuzzle();
// List<Integer> res = sol.findNumOfValidWords(new String[]{"apple", "pleas", "please"},
// new String[]{"aelwxyz", "aelpxyz", "aelpsxy", "saelpxy", "xaelpsy"});
List<Integer> res = sol.findNumOfValidWords(new String[]{"aaaa", "asas", "able", "ability", "actt", "actor", "access"},
new String[]{"aboveyz", "abrodyz", "abslute", "absoryz", "actresz", "gaswxyz"});
// new String[]{"ac", "ca"});
for (Integer i : res) {
System.out.println(i);
}
}
public List<Integer> findNumOfValidWords(String[] words, String[] puzzles) {
// 二进制表达, 压缩存储
Map<Integer, Integer> wordIdxCount = new HashMap<>();
for (String word : words) {
int wordIdx = 0;
for (int i = 0; i < word.length(); i++) {
int idx = word.charAt(i) - 'a';
wordIdx = wordIdx | (1 << idx);
}
int size = 0;
int tmp = wordIdx;
while (tmp > 0) {
size += (tmp & 1);
tmp >>= 1;
}
if (size <= 7) { // 因为谜面只有7位, 谜底超过7个不同的字符肯定是不合适的
wordIdxCount.put(wordIdx, wordIdxCount.getOrDefault(wordIdx, 0) + 1);
}
}
// 枚举判断, 取代遍历比较
List<Integer> result = new ArrayList<>(puzzles.length);
for (String puzzle : puzzles) {
int firstIdx = 1 << (puzzle.charAt(0) - 'a');
int puzzleIdx = 0;
for (int i = 0; i < puzzle.length(); i++) {
puzzleIdx = puzzleIdx | (1 << (puzzle.charAt(i) - 'a'));
}
Set<Integer> puzzleSubsets = new HashSet<>();
int tmp = puzzleIdx;
while (tmp > 0) {
puzzleSubsets.add(tmp | firstIdx); // 确保首位为1
tmp = (tmp - 1) & puzzleIdx;
}
int count = 0;
for (Integer puzzleSubIdx : puzzleSubsets) {
count += wordIdxCount.getOrDefault(puzzleSubIdx, 0);
}
result.add(count);
}
return result;
}
public List<Integer> findNumOfValidWords_tle2(String[] words, String[] puzzles) {
Map<Integer, Integer> wordIdxCount = new HashMap<>();
for (String word : words) {
int wordIdx = 0;
for (int i = 0; i < word.length(); i++) {
int idx = word.charAt(i) - 'a';
wordIdx = wordIdx | (1 << idx);
}
int size = 0;
int tmp = wordIdx;
while (tmp > 0) {
size += (tmp & 1);
tmp >>= 1;
}
if (size <= 7) { // 因为谜面只有7位, 谜底超过7个不同的字符肯定是不合适的
wordIdxCount.put(wordIdx, wordIdxCount.getOrDefault(wordIdx, 0) + 1);
}
}
List<Integer> result = new ArrayList<>(puzzles.length);
for (String puzzle : puzzles) {
int firstIdx = 1 << (puzzle.charAt(0) - 'a');
int puzzleIdx = 0;
for (int i = 0; i < puzzle.length(); i++) {
puzzleIdx = puzzleIdx | (1 << (puzzle.charAt(i) - 'a'));
}
int count = 0;
for (Integer wordIdx : wordIdxCount.keySet()) {
if ((wordIdx & firstIdx) < 1) {
continue;
}
int xor = puzzleIdx ^ wordIdx;
int not = ~puzzleIdx;
if ((xor & not) > 0) {
continue;
}
count += wordIdxCount.get(wordIdx);
}
result.add(count);
}
return result;
}
// TLE
public List<Integer> findNumOfValidWords_tle(String[] words, String[] puzzles) {
List<Set<Character>> wordSets = new ArrayList<>(words.length);
for (String word : words) {
Set<Character> set = new HashSet<>();
for (int i = 0; i < word.length(); i++) {
set.add(word.charAt(i));
}
wordSets.add(set);
}
List<Integer> result = new ArrayList<>(puzzles.length);
for (String puzzle : puzzles) {
boolean[] arr = new boolean[26];
for (int i = 0; i < puzzle.length(); i++) {
arr[puzzle.charAt(i) - 'a'] = true;
}
int count = 0;
for (int i = 0; i < words.length; i++) {
Set<Character> set = wordSets.get(i);
if (!set.contains(puzzle.charAt(0))) {
continue;
}
boolean flag = true;
for (Character c : set) {
if (!arr[c - 'a']) {
flag = false;
break;
}
}
if (flag) {
count++;
}
}
result.add(count);
}
return result;
}
}