Top 94% python solution


  • 0
    H

    '''Recursive solution:

        def countAndSay(self, n):
            if n == 2: return '11' 
            s = '1'
            for i in range(1,n):
                s = self.count(s)
            return s
    
        def count(self,string):
            newstr = ''
            count,start = 0,string[0]
            for i in range(len(string)):
                if string[i] == start:
                    count += 1
                    if i == len(string)-1:
                        newstr += (str(count) + string[i-1])
                else: 
                    newstr += (str(count) + string[i-1])
                    start = string[i]
                    count = 1
                    if i == len(string)-1:
                        newstr += (str(count) + string[i])
            return newstr
    

    '''


Log in to reply
 

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