Python Solution, O(n) time 259ms


  • 0
    H

    My idea is moving the symmetric line from the left to the right.

    class Solution(object):
        def countBinarySubstrings(self, s):
            length = len(s)
            ans = 0
            for index in range(length-1):
                if s[index] != s[index+1]:
                    ans += 1
                    step = 1
                    left_index = index - step
                    right_index = index +step+1
                    while left_index >=0 and right_index < len(s):
                        if s[left_index]==s[index] and s[index+1]==s[right_index]:
                            ans += 1
                        else:
                            break
                        step += 1
                        left_index = index - step
                        right_index = index + step + 1
            return ans
    

  • 0
    H
    This post is deleted!

Log in to reply
 

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