力扣算法题-猜字谜

外国友人仿照中国字谜设计了一个英文版猜字谜小游戏,请你来猜猜看吧。

字谜的迷面 puzzle 按字符串形式给出,如果一个单词 word 符合下面两个条件,那么它就可以算作谜底:

单词 word 中包含谜面 puzzle 的第一个字母。
单词 word 中的每一个字母都可以在谜面 puzzle 中找到。
例如,如果字谜的谜面是 "abcdefg",那么可以作为谜底的单词有 "faced", "cabbage", 和 "baggage";而 "beefed"(不含字母 "a")以及 "based"(其中的 "s" 没有出现在谜面中)。
返回一个答案数组 answer,数组中的每个元素 answer[i] 是在给出的单词列表 words 中可以作为字谜迷面 puzzles[i] 所对应的谜底的单词数目。

 

示例:

输入:
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] 所包含的字符都不重复。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-valid-words-for-each-puzzle
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

第一种思路:

暴力解,

把puzzle和word都转集合,然后逐个判断word的每个字母是不是都在puzzle里出现过。

(力扣提交算法提示超时......)

 1 class Solution(object):
 2     def findNumOfValidWords(self, words, puzzles):
 3         """
 4         :type words: List[str]
 5         :type puzzles: List[str]
 6         :rtype: List[int]
 7         """
 8         res = []
 9         for p in puzzles:
10             record_p = set(p)
11             cnt = 0
12             for word in words:
13                 set_word = set(word)
14                 if p[0] not in set_word:
15                     continue
16                 flag = 0
17                 for char in set_word:
18                     if char not in record_p:
19                         flag = 1
20                         break
21                 if not flag:
22                     cnt += 1
23             # print cnt, flag
24             res.append(cnt)
25         return res

第二种思路:

首先要明确一点,words里的word都可以转化成一个pattern,

比如 “aaaa”实际上等价于 pattern "a", “baaaa” == pattern "ab", “ababbabaabb” == pattern"ab",

所以先把所有的word,转成pattern之后,再统计所有pattern出现的频率。

接着,对于所有的puzzle,我们可以生成它的全部子集,

因为题目给定puzzle的长度为7,而且要求puzzle[0]必须出现在有效的子集里,

所以一共可以生成 2 ^ 6 = 64种组合结果,即除下标为0外的每一位都出现或者不出现在子集中。

生成完全部有效的组合之后,对于每个有效组合,

再在pattern的统计频率的字典里查找,当前的组合有没有出现过,如果出现过,就把对应频率加在答案里。

 1 class Solution(object):
 2     def findNumOfValidWords(self, words, puzzles):
 3         """
 4         :type words: List[str]
 5         :type puzzles: List[str]
 6         :rtype: List[int]
 7         """
 8         from collections import Counter
 9         for i in range(len(words)):
10             words[i] = "".join(sorted(set(words[i])))
11         record = Counter(words) #统计每种pattern出现的频率
12         
13         res = []
14         for p in puzzles:
15             bfs = [p[0]] #固定首字母
16             for char in p[1:]:
17                 bfs += [s + char for s in bfs] #生成64种可能
18             cnt = 0
19             for combination in bfs:
20                 tmp = "".join(sorted(combination))
21                 if tmp in record: #看看当前pattern在words里有没有出现过
22                     cnt += record[tmp]
23             res.append(cnt)
24         return res

 

posted @ 2019-09-03 17:10  charseki  阅读(545)  评论(0编辑  收藏  举报