38 Count and Say

class Solution(object):
    def countAndSay(self, n):
        """
        :type n: int
        :rtype: str
        """
        def countandsay(n):
            """
            :type n: str
            :rtype: str
            """
            p = 0
            ans = ""
            while p < len(n):
                c = 0 
                ch = n[p]
                while p < len(n) and n[p] == ch:
                    p += 1
                    c += 1
                ans = ans + str(c) + ch
            return ans
        if n == 1:
            return "1"
        else:
            ans = "1"
            for i in range(n-1):
                ans = countandsay(ans)
            return ans

results matching ""

    No results matching ""