Expressive Words LeetCode solution in Python

1
class Solution:
2
def expressiveWords(self, s: str, words: List[str]) -> int:
3
# edge cases
4
if len(s) == 0 and len(words) != 0:
5
return False
6
if len(words) == 0 and len(s) != 0:
7
return False
8
if len(s) == 0 and len(words) == 0:
9
return True
10

11
# helper function, compressing string and extract counts
12
def compressor(s_word):
13
init_string = [s_word[0]]
14
array = []
15
start = 0
16
for i, c in enumerate(s_word):
17
if c == init_string[-1]:
18
continue
19
array.append(i - start)
20
start = i
21
init_string += c
22
array.append(i - start + 1)
23
return init_string, array
24

25
res = len(words)
26
s_split, s_array = compressor(s)
27
for word in words:
28
word_split = [""]
29
word_array = []
30
word_split, word_array = compressor(word)
31
if s_split == word_split:
32
for num_s, num_word in zip(s_array, word_array):
33
if num_s != num_word and num_s < 3 or num_word > num_s:
34
res -= 1
35
break
36
else:
37
res -= 1
38
return res

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0