leeway00 / CodingTestPractice

0 stars 0 forks source link

Count and Say #9

Open leeway00 opened 2 years ago

leeway00 commented 2 years ago
    def countAndSay(self, n: int) -> str:
        if n == 1:
            return '1'
        out = ''
        s = self.countAndSay(n-1)
        count = 0
        for i in range(len(s)):
            c = s[i]
            if i == 0 or c == s[i-1]:
                count += 1
            else:
                out += str(count) + s[i-1]
                count = 1
        out += str(count) + c
        return out