猜字謎
難度:困難
外國友人仿照中國字謎設(shè)計了一個英文版猜字謎小游戲,請你來猜猜看吧旁蔼。
字謎的迷面 puzzle
按字符串形式給出适肠,如果一個單詞 word
符合下面兩個條件怜俐,那么它就可以算作謎底:
單詞word
中包含謎面 puzzle
的第一個字母效床。
單詞 word 中的每一個字母都可以在謎面puzzle
中找到睹酌。
例如,如果字謎的謎面是 "abcdefg"剩檀,那么可以作為謎底的單詞有 "faced", "cabbage", 和 "baggage"憋沿;而 "beefed"(不含字母 "a")以及 "based"(其中的 "s" 沒有出現(xiàn)在謎面中)。
返回一個答案數(shù)組 answer
沪猴,數(shù)組中的每個元素 answer[i]
是在給出的單詞列表 words
中可以作為字謎迷面 puzzles[i]
所對應(yīng)的謎底的單詞數(shù)目辐啄。
示例:
輸入:
words = ["aaaa","asas","able","ability","actt","actor","access"],
puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"]
輸出:[1,1,3,2,4,0]
解釋:
1 個單詞可以作為 "aboveyz" 的謎底 : "aaaa"
1 個單詞可以作為 "abrodyz" 的謎底 : "aaaa"
3 個單詞可以作為 "abslute" 的謎底 : "aaaa", "asas", "able"
2 個單詞可以作為 "absoryz" 的謎底 : "aaaa", "asas"
4 個單詞可以作為 "actresz" 的謎底 : "aaaa", "asas", "actt", "access"
沒有單詞可以作為 "gaswxyz" 的謎底,因為列表中的單詞都不含字母 'g'运嗜。
提示:
1 <= words.length <= 10^5
4 <= words[i].length <= 50
1 <= puzzles.length <= 10^4
puzzles[i].length == 7
-
words[i][j]
,puzzles[i][j]
都是小寫英文字母壶辜。 - 每個
puzzles[i]
所包含的字符都不重復(fù)。
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/number-of-valid-words-for-each-puzzle
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有洗出。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán)士复,非商業(yè)轉(zhuǎn)載請注明出處图谷。
題解:
思路:
關(guān)鍵點在于二進制狀態(tài)壓縮翩活。代碼:
public List<Integer> findNumOfValidWordsV1(String[] words, String[] puzzles) {
List<Integer> ans = new LinkedList<>();
for(int i = 0; i < puzzles.length; i++) {
int count = 0;
for (int j = 0; j < words.length; j++) {
//word包含puzzle的首字母
boolean condition1 = words[j].contains(puzzles[i].subSequence(0, 1));
//word中的每一個字母都能在puzzle中找到
boolean condition2 = false;
char[] wordChar = words[j].toCharArray();
int temp = wordChar.length;
for (char wc: wordChar) {
if (puzzles[i].contains(wc + "")) {
temp--;
}
}
if (temp == 0) {
condition2 = true;
}
if (condition1 && condition2) {
count++;
}
}
ans.add(i, count);
}
return ans;
}
public static List<Integer> findNumOfValidWordsV2(String[] words, String[] puzzles) {
List<Integer> ans = new LinkedList<>();
List<Set<Character>> list = new ArrayList<>();
for (String word: words) {
char[] arrays = word.toCharArray();
Set<Character> set = new HashSet<>();
for (char c: arrays) {
set.add(c);
}
list.add(set);
}
for(int i = 0; i < puzzles.length; i++) {
int count = 0;
for (int j = 0; j < list.size(); j++) {
Set<Character> set = list.get(j);
//word包含puzzle的首字母
boolean condition1 = set.contains(puzzles[i].charAt(0));
//word中的每一個字母都能在puzzle中找到
boolean condition2 = false;
int temp = set.size();
if (temp > 7) {
break;
}
for (char wc: set) {
if (puzzles[i].contains(wc + "")) {
temp--;
}
}
if (temp == 0) {
condition2 = true;
}
if (condition1 && condition2) {
count++;
}
}
ans.add(i, count);
}
return ans;
}
public static List<Integer> findNumOfValidWordsV3(String[] words, String[] puzzles) {
Map<Integer, Integer> map = new HashMap<>();
for (String word: words) {
int mask = 0;
char[] chars = word.toCharArray();
for (char c: chars) {
mask |= (1 << (c - 'a'));
}
if (Integer.bitCount(mask) <= 7) {
map.put(mask, map.getOrDefault(mask, 0) + 1);
}
}
List<Integer> ans = new ArrayList<Integer>();
for (String puzzle : puzzles) {
int total = 0;
int mask = 0;
for (int i = 1; i < 7; ++i) {
mask |= (1 << (puzzle.charAt(i) - 'a'));
}
int subset = mask;
do {
int s = subset | (1 << (puzzle.charAt(0) - 'a'));
if (map.containsKey(s)) {
total += map.get(s);
}
subset = (subset - 1) & mask;
} while (subset != mask);
ans.add(total);
}
return ans;
}
public static void main(String[] args) {
String[] words = {"aaaa","asas","able","ability","actt","actor","access"};
String[] puzzles = {"aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"};
System.err.println(findNumOfValidWordsV3(words, puzzles));
}