Count and Say LeetCode solution in Python

1
class Solution:
2
def countAndSay(self, n: int) -> str:
3
if n == 1:
4
return "1"
5

6
x = self.countAndSay(n - 1)
7
count = 1
8
cur = x[0]
9
res = ""
10
for i in range(1, len(x)):
11
if cur == x[i]:
12
count += 1
13
else:
14
res += str(count) + str(cur)
15
count = 1
16
cur = x[i]
17

18
res += str(count) + str(cur)
19
return res

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0