Leetcode Problem 696. Count Binary Substrings

696. Count Binary Substrings

Leetcode Solutions

Group By Character

  1. Initialize an array groups with the first element set to 1.
  2. Iterate through the string s starting from the second character.
    • If the current character is different from the previous one, append 1 to groups.
    • If the current character is the same as the previous one, increment the last element of groups by 1.
  3. Initialize ans to 0 to store the count of valid substrings.
  4. Iterate through groups starting from the second element.
    • Add the minimum of the current and previous elements to ans.
  5. Return ans as the final count of valid substrings.
UML Thumbnail

Linear Scan

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...