📌  相关文章
📜  具有相等的连续 0 和 1 的子串的计数(1)

📅  最后修改于: 2023-12-03 14:50:07.808000             🧑  作者: Mango

Title: Count of Substrings with Equal Number of Continuous 0s and 1s

Introduction

The count of substrings with equal numbers of continuous 0s and 1s is a common problem in string manipulation. Given a binary string, we need to determine the number of substrings that have an equal number of continuous 0s and 1s. This problem often comes up in various coding challenges and real-life scenarios where we need to analyze or manipulate binary data.

In this guide, we will discuss different approaches to solve this problem efficiently, including both the brute-force and optimized solutions. We will also provide code snippets in various programming languages to help developers understand and implement the solution.

Problem Statement

Given a binary string of length N, we need to count the number of substrings that have an equal number of continuous 0s and 1s.

Example: Input:

binaryString = "010011"

Output:

3

Explanation: The substrings with an equal number of continuous 0s and 1s are:

"01"
"0011"
"10"
Approach: Brute Force

One naive approach to solve this problem is to consider all possible substrings and count the ones that have an equal number of continuous 0s and 1s. Here are the steps for the brute-force approach:

  1. Initialize a counter variable count to 0.
  2. Iterate over all possible substrings of the input binary string.
  3. For each substring, count the number of 0s and 1s.
  4. If the count of 0s is equal to the count of 1s, increment the count variable.
  5. Return the final value of the count variable.

The time complexity of this approach is O(N^3) as we are considering all possible substrings. This approach is not efficient and will not work well for large input strings.

Approach: Optimized

To optimize the solution, we can use a slightly different approach. This approach will yield a time complexity of O(N), making it much more efficient. Here are the steps for the optimized approach:

  1. Initialize a counter variable count to 0 and a variable diff to 0.
  2. Iterate over the binary string character by character.
  3. If the current character is 0, increment diff by 1, otherwise decrement diff by 1.
  4. If diff becomes 0 at any point, increment count by 1.
  5. Return the final value of the count variable.

This approach works based on the observation that if we encounter an equal number of 0s and 1s in any substring, the difference between the count of 0s and 1s will be zero.

Code Snippets
Python
def count_substrings(binary_string):
    count = 0
    diff = 0

    for char in binary_string:
        if char == '0':
            diff += 1
        else:
            diff -= 1

        if diff == 0:
            count += 1

    return count
Java
public static int countSubstrings(String binaryString) {
    int count = 0;
    int diff = 0;

    for (char c : binaryString.toCharArray()) {
        if (c == '0') {
            diff++;
        } else {
            diff--;
        }

        if (diff == 0) {
            count++;
        }
    }

    return count;
}
JavaScript
function countSubstrings(binaryString) {
    let count = 0;
    let diff = 0;

    for (let i = 0; i < binaryString.length; i++) {
        const char = binaryString[i];

        if (char === '0') {
            diff++;
        } else {
            diff--;
        }

        if (diff === 0) {
            count++;
        }
    }

    return count;
}
Conclusion

The problem of counting substrings with an equal number of continuous 0s and 1s can be efficiently solved using the optimized approach presented in this guide. The optimized approach reduces the time complexity from O(N^3) to O(N), making it suitable for large input strings.