696. Count Binary Substrings
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to count the number of non-empty substrings in a binary string s that satisfy two conditions: 1) they have an equal number of 0s and 1s, and 2) all the 0s and all the 1s are grouped consecutively. For example, "0011" is a valid substring, but "0101" is not because the 0s and 1s are not grouped together.
Solution
Count Binary Substrings in Python
class Solution:
def countBinarySubstrings(self, s: str) -> int:
# If the string is too short, no valid substrings can be formed.
if len(s) <= 1:
return 0
count = 0
# Length of the previous group of consecutive characters.
prev_group_len = 0
# Length of the current group of consecutive characters.
current_group_len = 1
# Iterate through the string from the second character.
for i in range(1, len(s)):
if s[i] == s[i-1]:
# If the character is the same, extend the current group.
current_group_len += 1
else:
# If the character changes, we have a boundary between two groups.
# The number of valid substrings at this boundary is the minimum
# of the lengths of the two adjacent groups.
count += min(prev_group_len, current_group_len)
# The current group now becomes the previous group.
prev_group_len = current_group_len
# Start counting a new group.
current_group_len = 1
# After the loop, we need to account for the last boundary.
# This is between the very last group and the one before it.
count += min(prev_group_len, current_group_len)
return count
Count Binary Substrings in Java
class Solution {
public int countBinarySubstrings(String s) {
// If the string length is 1 or less, no such substrings can be formed.
if (s.length() <= 1) {
return 0;
}
int count = 0;
// Length of the previous group of consecutive characters.
int prevGroupLen = 0;
// Length of the current group of consecutive characters.
int currentGroupLen = 1;
// Iterate through the string from the second character.
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == s.charAt(i - 1)) {
// If the character is the same as the previous one, extend the current group.
currentGroupLen++;
} else {
// If the character changes, a new group starts.
// This forms a boundary between the previous group and the current one.
// The number of valid substrings is the minimum of their lengths.
count += Math.min(prevGroupLen, currentGroupLen);
// The current group becomes the previous group for the next iteration.
prevGroupLen = currentGroupLen;
// Reset current group length for the new group starting at s.charAt(i).
currentGroupLen = 1;
}
}
// After the loop, add the count for the last pair of groups.
count += Math.min(prevGroupLen, currentGroupLen);
return count;
}
}
Count Binary Substrings in C++
class Solution {
public:
int countBinarySubstrings(string s) {
// If the string length is 1 or less, no such substrings can be formed.
if (s.length() <= 1) {
return 0;
}
int count = 0;
// Length of the previous group of consecutive characters.
int prevGroupLen = 0;
// Length of the current group of consecutive characters.
int currentGroupLen = 1;
// Iterate through the string from the second character.
for (int i = 1; i < s.length(); ++i) {
if (s[i] == s[i-1]) {
// If the character is the same as the previous one, extend the current group.
currentGroupLen++;
} else {
// If the character changes, a new group starts.
// This forms a boundary between the previous group and the current one.
// The number of valid substrings is the minimum of their lengths.
count += min(prevGroupLen, currentGroupLen);
// The current group becomes the previous group for the next iteration.
prevGroupLen = currentGroupLen;
// Reset current group length for the new group starting at s[i].
currentGroupLen = 1;
}
}
// After the loop, add the count for the last pair of groups.
count += min(prevGroupLen, currentGroupLen);
return count;
}
};
Count Binary Substrings in TypeScript
function countBinarySubstrings(s: string): number {
// If the string length is 1 or less, no such substrings can be formed.
if (s.length <= 1) {
return 0;
}
let count: number = 0;
// Length of the previous group of consecutive characters.
let prevGroupLen: number = 0;
// Length of the current group of consecutive characters.
let currentGroupLen: number = 1;
// Iterate through the string from the second character.
for (let i = 1; i < s.length; i++) {
if (s[i] === s[i - 1]) {
// If the character is the same, extend the current group.
currentGroupLen++;
} else {
// If the character changes, a new group starts.
// This forms a boundary between the previous group and the current one.
// The number of valid substrings is the minimum of their lengths.
count += Math.min(prevGroupLen, currentGroupLen);
// The just-finished group becomes the "previous" group for the next boundary.
prevGroupLen = currentGroupLen;
// Reset the length for the new group.
currentGroupLen = 1;
}
}
// After the loop, add the count for the last pair of groups.
count += Math.min(prevGroupLen, currentGroupLen);
return count;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.