Given a binary string s, return the number of non-empty substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings are grouped consecutively.
Substrings that occur multiple times are counted the number of times they occur.
Example 1:
Input: s = "00110011" Output: 6 Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0011", "01", "1100", "10", "0011", and "01". Notice that some of these substrings repeat and are counted the number of times they occur. Also, "00110011" is not a valid substring because all the 0's (and 1's) are not grouped together.
Example 2:
Input: s = "10101" Output: 4 Explanation: There are 4 substrings: "10", "01", "10", "01" that have equal number of consecutive 1's and 0's.
Constraints:
1 <= s.length <= 105s[i] is either '0' or '1'.In this approach, we maintain two counters to track consecutive groups of 0's and 1's as we move through the string. By comparing these group counts, we can determine the number of valid equal-length substrings.
As we traverse the string, we increment the count for the current group and check if the previous group length is greater than or equal to the current group length. This indicates that we can form a valid substring.
The above code uses a two-pointer technique to track the lengths of consecutive characters. It keeps track of the current and previous group lengths and increases the output count based on the lesser of the two. This ensures only valid substrings with equal '0's and '1's are counted.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n) because we make a single pass through the string.
Space Complexity: O(1) since we use a constant amount of extra space.
This approach involves creating an array to store the lengths of consecutive 0's and 1's. By iterating through this array, we add the minimum value of each pair to a count, which represents valid substrings.
First, traverse the string to form groups, then iterate through the formed group to compute the valid substring count based on consecutive groups' minimum lengths.
In this C implementation, after creating a dynamically sized array to store consecutive group lengths, we compute valid substrings by iterating over these lengths and summing the minimum values of consecutive pairs.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n) - The string is traversed twice, but both passes are O(n).
Space Complexity: O(n) due to the need to store group lengths.
| Approach | Complexity |
|---|---|
| Two-Pointer Approach | Time Complexity: O(n) because we make a single pass through the string. |
| Group Count Array Approach | Time Complexity: O(n) - The string is traversed twice, but both passes are O(n). |
Count Binary Substrings | Live Coding with Explanation | Leetcode - 696 • Algorithms Made Easy • 23,496 views views
Watch 9 more video solutions →Practice Count Binary Substrings with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor