File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments