forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpressive-words.py
More file actions
28 lines (23 loc) · 782 Bytes
/
expressive-words.py
File metadata and controls
28 lines (23 loc) · 782 Bytes
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
# Time: O(n + s), n is the sum of all word lengths, s is the length of S
# Space: O(l + s), l is the max word length
import itertools
class Solution(object):
def expressiveWords(self, S, words):
"""
:type S: str
:type words: List[str]
:rtype: int
"""
# Run length encoding
def RLE(S):
return itertools.izip(*[(k, len(list(grp)))
for k, grp in itertools.groupby(S)])
R, count = RLE(S)
result = 0
for word in words:
R2, count2 = RLE(word)
if R2 != R:
continue
result += all(c1 >= max(c2, 3) or c1 == c2
for c1, c2 in itertools.izip(count, count2))
return result