-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode916.py
More file actions
45 lines (40 loc) · 1.38 KB
/
LeetCode916.py
File metadata and controls
45 lines (40 loc) · 1.38 KB
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
44
45
A = ['amazon', 'apple', 'facebook', 'google', 'leetcode']
B = ['e', 'o']
class WordSubSet:
@staticmethod
def solution(subset_a, subset_b):
universal = []
for char in subset_a:
for sub_char in subset_b:
char_temp = char
for word in sub_char:
if word not in char_temp:
break
else:
ind = char_temp.index(word)
char_temp = char_temp[:ind] + char_temp[ind + 1:]
else:
continue
break
else:
universal.append(char)
return universal
@staticmethod
def solution_update(subset_a, subset_b):
universal = []
from collections import Counter
count_char_in_subset_b = Counter()
for word in subset_b:
for (key, value) in Counter(word).items():
if count_char_in_subset_b[key] < value:
count_char_in_subset_b[key] = value
for word in subset_a:
string_letter_counts = Counter(word)
for (key, value) in count_char_in_subset_b.items():
if string_letter_counts[key] < value:
break
else:
universal.append(word)
return universal
S = WordSubSet()
print(S.solution_update(A, B))