Easy Python Solution, Constant Space


  • 0
    M
    class Solution(object):
        def countAndSay(self, n):
            """
            :type n: int
            :rtype: str
            """
        
            prev=""
            cur="1"
            
            for i in range(n-1):
                prev=cur
                cur=""
                cnt=1
                for s in range(len(prev)):
                    if s+1<len(prev) and prev[s]==prev[s+1]:
                        cnt+=1
                    else:
                        cur=cur+str(cnt)+str(prev[s])
                        cnt=1
            
            return cur
    

Log in to reply
 

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