Straightforward solution with Python


  • 0
    A
    class Solution(object):
        def countAndSay(self, n):
        	s = "1"
        	for i in xrange(n-1):
        		s = self.say(s)
        	return s 
            
    
        def say(self, s):
        	ans = ""
        	i = 0
        	while i < len(s):
        		count = 1
        		while i<len(s)-1 and s[i+1] == s[i]:
        			count += 1
        			i += 1
        		ans = ans + str(count) + s[i]
        		i += 1
        	return ans 
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.