classSolution(object):defcount(self,s):num=1ret=""fori,jinenumerate(s):ifi+1<len(s)ands[i]==s[i+1]:num+=1elifi+1<len(s):ret=ret+str(num)+s[i]num=1# last elementret=ret+str(num)+s[i]returnretdefcountAndSay(self,n):""" :type n: int :rtype: str """ret="1"foriinrange(1,n):ret=self.count(ret)returnretif__name__=="__main__":sol=Solution()sol.countAndSay(2)