Skip to content

Commit cd21802

Browse files
committed
Do daily
1 parent 9d7e84f commit cd21802

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

my-submissions/m38 v1 recursive.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def countAndSay(self, n: int) -> str:
3+
if n == 1 :
4+
return '1'
5+
6+
n_min_1 = self.countAndSay(n - 1)
7+
output = []
8+
9+
prev, cnt = None, 0
10+
for c in n_min_1 + "#" : # '#' acts as a delimiter
11+
if c == prev : # to append the final block
12+
cnt += 1
13+
else :
14+
if prev is not None :
15+
output.append(str(cnt))
16+
output.append(prev)
17+
prev, cnt = c, 1
18+
19+
return ''.join(output)

my-submissions/m38 v2 iterative.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def countAndSay(self, n: int) -> str:
3+
output = "1"
4+
for _ in repeat(None, n - 1) :
5+
curr, output = output, []
6+
prev, cnt = None, 0
7+
for c in curr + "#" :
8+
if c == prev :
9+
cnt += 1
10+
continue
11+
if prev is not None :
12+
output.extend([str(cnt), prev])
13+
prev, cnt = c, 1
14+
output = ''.join(output)
15+
return output

0 commit comments

Comments
 (0)