Beautiful Binary String

  • + 0 comments

    python 3

    def beautifulBinaryString(b):
        l = list(b)
        steps = 0
        
        for i in range(len(b)-2):
            if "".join(l[i : i + 3]) == '010':
                steps += 1
                if i + 3 < len(b) and l[i + 3] == '1':
                    l[i + 2] = '1'
                else:
                    l[i + 1] = '0'
        
        return steps