File tree 1 file changed +9
-8
lines changed
scripts/algorithms/F/Find Common Characters
1 file changed +9
-8
lines changed Original file line number Diff line number Diff line change
1
+ # Runtime: 87 ms (Top 42.12%) | Memory: 14.3 MB (Top 6.17%)
1
2
class Solution :
2
3
def _get_char_counts (self , s : str ) -> dict [str ]:
3
4
"""builds a dict of letters : count"""
4
5
d = {}
5
6
for i in s :
6
7
d [i ] = d .get (i ,0 )+ 1
7
8
return d
8
-
9
+
9
10
def commonChars (self , words : list [str ]) -> list [str ]:
10
11
"""returns a string of letters common between a list of words (including duplicates)"""
11
12
if not words :
12
13
return
13
-
14
+
14
15
# O(n^2)
15
16
words = [self ._get_char_counts (word ) for word in words ]
16
-
17
- # O(nm), set intersection
17
+
18
+ # O(nm), set intersection
18
19
common = words [0 ].keys ()
19
20
for other in words [1 :]:
20
21
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
23
24
result = []
24
25
for c in common :
25
26
result += [c ] * min (count [c ] for count in words )
26
-
27
- return result
27
+
28
+ return result
You can’t perform that action at this time.
0 commit comments