-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0038-count-and-say.py
More file actions
33 lines (28 loc) · 940 Bytes
/
0038-count-and-say.py
File metadata and controls
33 lines (28 loc) · 940 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
32
33
# time compexity: O(2^n)
# space complexity: O(n)
from collections import defaultdict
from functools import lru_cache
class Solution:
def RLE(self, countString: str):
temp = ""
freqDict = defaultdict(int)
freqDict[countString[0]] += 1
for i in range(1, len(countString)):
if countString[i] != countString[i-1]:
key = countString[i-1]
freq = freqDict[countString[i-1]]
temp += str(freq) + key
del freqDict[countString[i-1]]
freqDict[countString[i]] += 1
else:
freqDict[countString[i]] += 1
for key, freq in freqDict.items():
temp += str(freq) + key
return temp
@lru_cache(None)
def countAndSay(self, n: int) -> str:
if n == 1:
return "1"
return self.RLE(self.countAndSay(n-1))
n = 4
print(Solution().countAndSay(n))