Short and simple python with boolean


  • 0
    R
    class Solution(object):
        def magicalString(self, n):
            s = '122'
            idx = 2
            bl = True
            while len(s) <= n:
                if s[idx] == '1':
                    s += '1' if bl else '2'
                else:
                    s += '11' if bl else '22'
                idx += 1
                if bl:
                    bl = False
                else:
                    bl = True
    
            c = collections.Counter(s[:n])
            return c['1']
            
    

Log in to reply
 

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