-
Notifications
You must be signed in to change notification settings - Fork 252
Count Binary Substrings
JCSU Unit 15 Problem Set 1 (Click for link to problem statements)
- 💡 Difficulty: Medium
- ⏰ Time to complete: 20-25 mins
- 🛠️ Topics: Strings, Sliding Window, Grouping
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
s
that 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
0
and1
.
- 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.
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.
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.
- Initialize variables:
-
prev_group_len
to track the length of the previous group of characters. -
curr_group_len
to track the length of the current group of characters. -
count
to 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_len
andcurr_group_len
tocount
. - Update
prev_group_len
tocurr_group_len
. - Reset
curr_group_len
to 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.
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
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
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.