748. 最短补全词
为保证权益,题目请参考 748. 最短补全词(From LeetCode).
解决方案1
Python
python
# 748. 最短补全词
# https://leetcode-cn.com/problems/shortest-completing-word/
from typing import List
class Solution:
def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str:
pattern = {}
for l in licensePlate:
if l.isalpha():
if l.lower() in pattern:
pattern[l.lower()] += 1
else:
pattern[l.lower()] = 1
ans = None
for word in words:
t = pattern.copy()
for w in word:
if w in t:
t[w] -= 1
isOk = True
for k, v in t.items():
if v > 0:
isOk = False
break
if isOk is not True:
continue
if ans is None or len(word) < len(ans):
ans = word
return ans
if __name__ == "__main__":
solution = Solution()
print(solution.shortestCompletingWord("GrC8950",["according","level","meeting","none","marriage","rest"]))
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
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