Skip to content

Commit 02482e7

Browse files
committed
Runtime: 87 ms (Top 42.12%) | Memory: 14.3 MB (Top 6.17%)
1 parent 1271955 commit 02482e7

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1+
# Runtime: 87 ms (Top 42.12%) | Memory: 14.3 MB (Top 6.17%)
12
class Solution:
23
def _get_char_counts(self, s: str) -> dict[str]:
34
"""builds a dict of letters : count"""
45
d = {}
56
for i in s:
67
d[i] = d.get(i,0)+1
78
return d
8-
9+
910
def commonChars(self, words: list[str]) -> list[str]:
1011
"""returns a string of letters common between a list of words (including duplicates)"""
1112
if not words:
1213
return
13-
14+
1415
# O(n^2)
1516
words = [self._get_char_counts(word) for word in words]
16-
17-
# O(nm), set intersection
17+
18+
# O(nm), set intersection
1819
common = words[0].keys()
1920
for other in words[1:]:
2021
common &= other.keys()
21-
22-
# O(nm), number of common characters across the number of words
22+
23+
# O(nm), number of common characters across the number of words
2324
result = []
2425
for c in common:
2526
result += [c] * min(count[c] for count in words)
26-
27-
return result
27+
28+
return result

0 commit comments

Comments
 (0)