Skip to content

Latest commit

 

History

History
23 lines (21 loc) · 509 Bytes

038.md

File metadata and controls

23 lines (21 loc) · 509 Bytes

38. Count and Say

Solution 1

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