Skip to content

Commit bcceb58

Browse files
authored
Create 916._Word_Subsets.md
1 parent afddd2b commit bcceb58

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# 916. Word Subsets
2+
3+
**<font color=red>难度: Medium</font>**
4+
5+
## 刷题内容
6+
7+
> 原题连接
8+
9+
* https://leetcode.com/contest/weekly-contest-104/problems/word-subsets/
10+
11+
> 内容描述
12+
13+
```
14+
We are given two arrays A and B of words. Each word is a string of lowercase letters.
15+
16+
Now, say that word b is a subset of word a if every letter in b occurs in a, including multiplicity. For example, "wrr" is a subset of "warrior", but is not a subset of "world".
17+
18+
Now say a word a from A is universal if for every b in B, b is a subset of a.
19+
20+
Return a list of all universal words in A. You can return the words in any order.
21+
22+
23+
24+
Example 1:
25+
26+
Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["e","o"]
27+
Output: ["facebook","google","leetcode"]
28+
Example 2:
29+
30+
Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["l","e"]
31+
Output: ["apple","google","leetcode"]
32+
Example 3:
33+
34+
Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["e","oo"]
35+
Output: ["facebook","google"]
36+
Example 4:
37+
38+
Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["lo","eo"]
39+
Output: ["google","leetcode"]
40+
Example 5:
41+
42+
Input: A = ["amazon","apple","facebook","google","leetcode"], B = ["ec","oc","ceo"]
43+
Output: ["facebook","leetcode"]
44+
45+
46+
Note:
47+
48+
1 <= A.length, B.length <= 10000
49+
1 <= A[i].length, B[i].length <= 10
50+
A[i] and B[i] consist only of lowercase letters.
51+
All words in A[i] are unique: there isn't i != j with A[i] == A[j].
52+
```
53+
54+
## 解题方案
55+
56+
> 思路 1
57+
******- 时间复杂度: O(max(ma*la, mb*lb))******- 空间复杂度: O(1)******
58+
59+
60+
假设A的长度为na,其中最长的一个单词长度为la
61+
B的长度为nb,其中最长的一个单词长度为lb
62+
63+
那么时间复杂度为O(max(ma*la, mb*lb)),由于字符总共就26个,所以空间复杂度可以看作O(1)
64+
65+
66+
先求出B中所有字符在所有单词中出现的最大次数,用lookup记录,然后如果A中的某一个单词满足lookup里面所有的key都有,且对应数量都大于等于lookup中的,
67+
那么该单词满足条件,append到最终结果中
68+
69+
```python
70+
class Solution(object):
71+
def wordSubsets(self, A, B):
72+
"""
73+
:type A: List[str]
74+
:type B: List[str]
75+
:rtype: List[str]
76+
"""
77+
if not B or len(B) == 0:
78+
return A
79+
lookup = collections.Counter(B[0])
80+
for word in B[1:]:
81+
tmp = collections.Counter(word)
82+
for key in tmp.keys():
83+
if key not in lookup:
84+
lookup[key] = tmp[key]
85+
else:
86+
lookup[key] = max(lookup[key], tmp[key])
87+
def uni(tmp_a, tmp_b):
88+
if len(tmp_a.keys()) < len(tmp_b.keys()):
89+
return False
90+
for key in tmp_b.keys():
91+
if key not in tmp_a:
92+
return False
93+
else:
94+
if tmp_a[key] < tmp_b[key]:
95+
return False
96+
return True
97+
res = []
98+
for word in A:
99+
if uni(collections.Counter(word), lookup):
100+
res.append(word)
101+
return res
102+
```
103+
104+
105+
106+
107+
108+
109+
110+
111+
112+
113+
114+
115+
116+
117+

0 commit comments

Comments
 (0)