Skip to content

Commit 99fca3a

Browse files
committed
Jan 10
1 parent 4ce7687 commit 99fca3a

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

2025-01-January-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
| January 7 | [1408. String Matching in an Array](https://leetcode.com/problems/string-matching-in-an-array/) | Easy | Solved |
1313
| January 8 | [3042. Count Prefix and Suffix Pairs I](https://leetcode.com/problems/count-prefix-and-suffix-pairs-i/) | Easy | Solved |
1414
| January 9 | [2185. Counting Words With a Given Prefix](https://leetcode.com/problems/counting-words-with-a-given-prefix/) | Easy | Solved |
15-
| January 10 | []() | | |
15+
| January 10 | [916. Word Subsets](https://leetcode.com/problems/word-subsets/) | Medium | Solved |
1616
| January 11 | []() | | |
1717
| January 12 | []() | | |
1818
| January 13 | []() | | |
@@ -40,5 +40,5 @@
4040
| Level | Problems | Solved | Unsolved |
4141
| --- | --- | --- | --- |
4242
| Easy | 4 | 4 | 0 |
43-
| Medium | 5 | 5 | 0 |
43+
| Medium | 6 | 6 | 0 |
4444
| Hard | 0 | 0 | 0 |
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from collections import Counter, defaultdict
2+
from typing import List
3+
4+
5+
class Solution:
6+
def wordSubsets(self, words1: List[str], words2: List[str]) -> List[str]:
7+
req_freq = defaultdict(int)
8+
for word in words2:
9+
for ch, count in Counter(word).items():
10+
req_freq[ch] = max(req_freq[ch], count)
11+
12+
result = []
13+
for word in words1:
14+
freq = Counter(word)
15+
for ch, count in req_freq.items():
16+
if freq[ch] < count:
17+
break
18+
else:
19+
# for ... else instead of using a flag
20+
result.append(word)
21+
return result
22+
23+
24+
def main():
25+
words1 = ['amazon', 'apple', 'facebook', 'google', 'leetcode']
26+
words2 = ['e', 'o']
27+
assert Solution().wordSubsets(words1, words2) == ['facebook', 'google', 'leetcode']
28+
29+
words1 = ['amazon', 'apple', 'facebook', 'google', 'leetcode']
30+
words2 = ['l', 'e']
31+
assert Solution().wordSubsets(words1, words2) == ['apple', 'google', 'leetcode']
32+
33+
34+
if __name__ == '__main__':
35+
main()

0 commit comments

Comments
 (0)