The basic idea is that, given a string , I use two pointers to group the string into multiple groups. For example, given '1211', it is split into '1'(count = 1), '2'(count = 1) and '1'(count = 2), therefore you can get str(count) + 'character' to build the return string.
Here, I just use recursion to implement cos this is the very first thought; of course, you can use for-loop to compact it.
I am a newbie here and open to all suggestions and criticism.
Here attaches my code:
class Solution(object): def countAndSay(self, n): if not n or n <= 0: return '' if n == 1: return '1' return self.group(self.countAndSay(n - 1)) def group(self, s1): # 1211 now = 0 last = s1 count = 0 rs = '' for i in range(len(s1)): now = s1[i] if now == last: count += 1 else: rs += str(count) + str(last) count = 1 last = now rs += str(count) + str(last) return rs