easy recursion python 36ms


  • 0
    W

    suppose we know the correct string for n-1, just build n from n-1
    '''

    def countAndSay(self, n):
        if n == 1: return '1'
        string = self.countAndSay(n - 1)
        index, count, ret = 0, 1, ''
        while (index + 1 < len(string)):
            if string[index] == string[index + 1]:
                count += 1
            else:
                ret += str(count) + string[index]
                count = 1
            index += 1
        ret += str(count) + string[index]   
        return ret
    

    '''


Log in to reply
 

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