Advertisement

Count Binary Substrings - LeetCode 696 Solution

Count Binary Substrings - Complete Solution Guide

Count Binary Substrings is LeetCode problem 696, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 ar

Detailed Explanation

The problem requires us to find the number of non-empty substrings in a binary string `s` that have an equal number of consecutive 0s and 1s. A crucial constraint is that the 0s and 1s must be grouped consecutively. For instance, "0011" and "10" are valid, but "0101" is not because the 0s and 1s are not grouped together. The output should be the total count of such valid substrings, counting duplicates as many times as they appear.

Solution Approach

The solution involves two main steps. First, we iterate through the string and identify the consecutive groups of identical characters (0s or 1s). We record the lengths of these groups. Second, we iterate through the list of group lengths and calculate the number of valid substrings at the boundaries of adjacent groups by taking the minimum of their lengths. We accumulate these counts to arrive at the final answer.

Step-by-Step Algorithm

  1. Step 1: Initialize an empty list or array `groups` (or two variables `prev` and `curr`) to store the lengths of consecutive groups of 0s or 1s.
  2. Step 2: Initialize a counter `count` to 1 to track the length of the current group.
  3. Step 3: Iterate through the string `s` from the second character (index 1) to the end.
  4. Step 4: For each character, compare it to the previous character. If they are the same, increment the `count` by 1 (the current group is extending).
  5. Step 5: If the current character is different from the previous character, it indicates the end of the previous group. Append the current `count` to the `groups` list (or update `prev` with the current `curr` and set `curr` to 1), and reset `count` to 1 to start counting the new group.
  6. Step 6: After the loop finishes, append the final `count` to the `groups` list (or handle the edge case for the last group if using the `prev` and `curr` variables).
  7. Step 7: Initialize a variable `ans` to 0 to store the total number of valid substrings.
  8. Step 8: Iterate through the `groups` list from the second element (index 1) to the end.
  9. Step 9: For each element, calculate the minimum of the current group length and the previous group length (groups[i-1] and groups[i]). Add this minimum value to `ans`.
  10. Step 10: Return the final value of `ans`.

Key Insights

  • Insight 1: The core idea is to identify consecutive groups of identical characters (either 0s or 1s). For example, in "0011100", the groups are [2, 3, 2] corresponding to "00", "111", and "00".
  • Insight 2: Once the lengths of these consecutive groups are determined, the number of valid substrings formed at the boundary of two adjacent groups is the minimum of the lengths of the two groups. For example, if we have groups [2, 3], we can form `min(2, 3) = 2` valid substrings: "0011" and "01".
  • Insight 3: We can avoid using extra space for a `groups` array by keeping track of the previous and current group size only.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Two Pointers, String.

Companies

Asked at: ByteDance, IBM, J.P. Morgan, Morgan Stanley, Salesforce.