-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcount2.py
More file actions
31 lines (24 loc) · 780 Bytes
/
count2.py
File metadata and controls
31 lines (24 loc) · 780 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
28
29
30
31
class Solution:
def countAndSay(self, n: int) -> str:
def say(nStr):
ns = list(nStr)
stack = []
saying = ""
while ns:
#print(ns)
curr = ns.pop(0)
if stack and stack[-1] == curr:
stack.append(curr)
else:
if stack:
saying += str(len(stack))+stack[-1]
stack = [curr]
if stack:
saying += str(len(stack))+stack[-1]
return saying
self.memo = {1:"1"}
for i in range(2, n+1):
result = say(self.memo[i-1])
self.memo[i] = result
#print(self.memo)
return self.memo[n]