-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path038_CountAndSay.py
49 lines (43 loc) · 1.21 KB
/
038_CountAndSay.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
def countAndSay(self, n):
s = '1'
for _ in range(n - 1):
s = ''.join(str(len(list(group))) + digit
for digit, group in itertools.groupby(s))
return s
def countAndSay(self, n):
s = '1'
for _ in range(n - 1):
s = ''.join(str(len(group)) + digit
for group, digit in re.findall(r'((.)\2*)', s))
return s
#Method from LeetCode
def countAndSay(self, n):
s = '1'
for _ in range(n - 1):
s = re.sub(r'(.)\1*', lambda m: str(len(m.group(0))) + m.group(1), s)
return s
#65.14%
class Solution(object):
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
if 1 > n:
return ""
if 1 == n:
return "1"
rst = ""
prev = self.countAndSay(n - 1)
prev = prev + '#'
prevChar = prev[0]
times = 1
for c in prev[1:]:
if c == prevChar:
times = times + 1
else:
rst = rst + str(times)
rst = rst + prevChar
prevChar = c
times = 1
return rst