My 66ms Python solution


  • -1
    S
    class Solution:
        # @return a string
        def countAndSay(self, n):
              string = output = ['1']
              
              for i in range(1, n):
                prev = None
                count = 0
                output = []
                
                for j in string:
                  if not prev:
                    prev = j
                    count += 1
                  elif j != prev:
                    output.append(str(count))
                    output.append(prev)
                    prev = j
                    count = 1
                  elif j == prev:
                    prev = j
                    count += 1
                else:
                  output.append(str(count))
                  output.append(prev)
                  string = output
              
              return ''.join(output)

Log in to reply
 

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