-
Notifications
You must be signed in to change notification settings - Fork 0
/
809_Expressive_Words.py
55 lines (44 loc) · 1.44 KB
/
809_Expressive_Words.py
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
46
47
48
49
50
51
52
53
54
55
from collections import Counter
class Solution:
def expressiveWords(self, S: str, words: List[str]) -> int:
if not S or not words:
return 0
def FindString(string):
str_Stack = []
count_Stack = []
cur = string[0]
count = 1
for t, i in enumerate(string[1:]):
if i == cur:
count += 1
else:
str_Stack.append(cur)
count_Stack.append(count)
cur = i
count = 1
str_Stack.append(cur)
count_Stack.append(count)
return str_Stack, count_Stack
S_Stack, S_count = FindString(S)
result = 0
for string in words:
cur_Stack, cur_count = FindString(string)
find = 1
if len(S_Stack) != len(cur_Stack):
break
for i in range(len(S_Stack)):
if cur_Stack[i] != S_Stack[i]:
find = 0
break
if cur_count[i] > S_count[i]:
find = 0
break
if cur_count[i] < S_count[i] and S_count[i] < 3:
find = 0
break
result += find
return result
'''
comments:
1. when you write a string to #s, pay attention to the last word. For example: 'helllo', should be 1h1e2l10
'''