The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...

1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
Given an integer n, generate the nth sequence.

Note: The sequence of integers will be represented as a string.

# How to prove the count should less than 10
# https://leetcode.com/discuss/6762/how-to-proof-the-count-is-always-less-than-10

class Solution:
    # @return a string
    def countAndSay(self, n):
        s = '1'
        for i in xrange(1,n):  # Note: here start from 1 
            s = self.count(s)
        return s 
    
    def count(self,s):
        amount = 1
        curr = '#'
        result = ''
        for char in s:
            if char != curr:
                if curr != '#':
                    result += str(amount) + curr  # Note: here we should use result += str(amount) + curr
                curr = char
                amount = 1
            else:
                amount +=1
        result += str(amount) + curr
        return result 


@@ -454,16 +454,471 @@
 han-10%0A%0A
+class Solution:%0A    # @param %7Binteger%7D n%0A    # @return %7Bstring%7D%0A    def countAndSay(self, n):%0A        ret = '1'%0A        for i in xrange(1, n):%0A            s = %22%22; j = 0%0A            while j %3C len(ret):%0A                next = j%0A                while next %3C len(ret) and ret%5Bnext%5D == ret%5Bj%5D:%0A                    next += 1%0A                s += str(next-j)%0A                s += ret%5Bj%5D%0A                j = next%0A            ret = s%0A        return ret%0A        %0A%0A%0A
 class So

Update Count_and_Say.py