Easy to understand Python solution


  • 0

    def countAndSay(self, n):

        term = count = 1
        ar=[term]
        temp=[]
        while term<n:
            for i, num in enumerate(ar):
                if i + 1 < len(ar):
                    if ar[i] == ar[i+1]:
                        count += 1
                    else:
                        temp.append(count)
                        temp.append(num)
                        count = 1
                else:
                    temp.append(count)
                    temp.append(num)
                    count = 1
            ar = temp
            temp=[]
            term += 1
        
        return ''.join(str(s) for s in ar)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.