Easy straightfwd Python3 solution using itertools


#1

Using groupby

from itertools import groupby
def gen_N(A):
if A == 1:
return ‘1’
elif A == 2:
return ‘11’
elif A == 3:
return ‘21’

# Recursive call
seed = gen_N(A-1)

ret = ''
# Using groupby to generate groups
for i,n in groupby(seed):
    # Appending counts
    ret += str(len(list(n)))+i[0]
        
return ret

class Solution:
# @param A : integer
# @return a strings
def countAndSay(self, A):
# Returning output
return gen_N(A)