Count Binary Substrings
JCSU Unit 15 Problem Set 1 (Click for link to problem statements)
Problem Highlights
- π‘ Difficulty: Medium
- β° Time to complete: 20-25 mins
- π οΈ Topics: Strings, Sliding Window, Grouping
1: U-nderstand
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
- What is the goal of the problem?
- Count the number of substrings in the binary string
sthat have an equal number of0βs and1βs grouped consecutively.
- Count the number of substrings in the binary string
- Are there constraints on input?
- The string consists only of
0and1.
- The string consists only of
HAPPY CASE
Input:
s = β00110011β
Output:
6
Explanation:
Substrings: β0011β, β01β, β1100β, β10β, β0011β, β01β.
EDGE CASE
Input:
s = β10101β
Output:
4
Explanation:
Substrings: β10β, β01β, β10β, β01β.
EDGE CASE
Input:
s = β1111β
Output:
0
Explanation:
No valid substrings because there are no consecutive groups of 0βs and 1βs.
2: M-atch
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For counting consecutive group substrings, we want to consider the following approaches:
- Sliding Window Approach: Track lengths of consecutive groups of
0βs and1βs. - Greedy Method: Use two counters to compare current and previous group lengths.
3: P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
Traverse the string and calculate lengths of consecutive groups of 0βs and 1βs. Add the minimum of the previous and current group lengths to the count.
Steps:
- Initialize variables:
prev_group_lento track the length of the previous group of characters.curr_group_lento track the length of the current group of characters.countto store the total number of valid substrings.
- Traverse the string starting from the second character:
- If the current character matches the previous one, increase
curr_group_len. - Otherwise:
- Add the minimum of
prev_group_lenandcurr_group_lentocount. - Update
prev_group_lentocurr_group_len. - Reset
curr_group_lento 1.
- Add the minimum of
- If the current character matches the previous one, increase
- Add the last pair of groups to
count. - Return the total count.
4: I-mplement
Implement the code to solve the algorithm.
def count_binary_substrings(s):
# Initialize variables to store the lengths of consecutive groups
prev_group_len = 0
curr_group_len = 1
count = 0
# Traverse the string to calculate the lengths of consecutive groups of '0's and '1's
for i in range(1, len(s)):
if s[i] == s[i - 1]: # If the current character matches the previous one, increase the current group length
curr_group_len += 1
else:
# If characters change, add the minimum of the previous and current group lengths to the count
count += min(prev_group_len, curr_group_len)
prev_group_len = curr_group_len # Update the previous group length
curr_group_len = 1 # Reset the current group length
# Add the last pair of groups to the count
count += min(prev_group_len, curr_group_len)
return count
5: R-eview
Review the code by running specific example(s) and recording values (watchlist) of your codeβs variables along the way.
Example 1:
- Input: s = β00110011β
- Expected Output: 6
- Observed Output: 6
Example 2:
- Input: s = β10101β
- Expected Output: 4
- Observed Output: 4
Example 3:
- Input: s = β1111β
- Expected Output: 0
- Observed Output: 0
6: E-valuate
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume n is the length of the input string.
- Time Complexity: O(n) because we traverse the string once.
- Space Complexity: O(1) because we use constant additional space for variables.