Count Valid Prefixes - Solution & Explanation
Problem Statement
You are given a binary string s.
A prefix of s is considered valid if its characters can be rearranged to form an alternating string.
Return the number of valid prefixes of s.
A binary string is a string consisting only of '0' and '1'.
A prefix of a string is a substring that starts from the beginning of the string and extends to any point within it.
A substring is a contiguous non-empty sequence of characters within a string.
A string is considered alternating if no two adjacent characters are equal.
Example 1:
Input: s = "00101"
Output: 3
Explanation:
The valid prefixes are:
"0": It is already an alternating string."001": It can be rearranged into"010", which is an alternating string."00101": It can be rearranged into"01010", which is an alternating string.
Thus, the answer is 3.
Example 2:
Input: s = "101"
Output: 3
Explanation:
All prefixes of s = "101" are already alternating strings. Thus, the answer is 3.
Constraints:
1 <= s.length <= 100sconsists only of'0'and'1'.
Approach Overview
Problem Overview: Given an array of integers, count how many prefixes of the array are valid, where a valid prefix meets a specific condition (e.g., sum is positive).
Approach 1: Brute Force (O(n^2))
Check every possible prefix by iterating through the array and validating each prefix from the start up to the current index. This approach uses nested loops, making it inefficient for large arrays. Prefer this only for understanding the problem basics.
Approach 2: Single Pass with Tracking (O(n))
Iterate through the array once, maintaining a running sum or other condition tracker. For each element, update the tracker and check if the current prefix meets the validity condition. This approach leverages array iteration and prefix sum techniques for optimal performance.
Recommended for interviews: The single-pass approach is expected in interviews. It demonstrates efficient problem-solving and understanding of array traversal. Brute force shows foundational knowledge, but optimal solutions are preferred.
Solution
A string can be rearranged into an alternating string if and only if the counts of '0' and '1' in it differ by at most 1.
Therefore, we traverse the string s and maintain a variable t equal to the number of '1's minus the number of '0's in the current prefix (increment by one on '1', decrement by one on '0'). If |t| leq 1, the current prefix is valid, and we add one to the answer.
The time complexity is O(n), where n is the length of the string s. The space complexity is O(1).
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force | O(n^2) | O(1) | Small arrays or understanding basics |
| Single Pass | O(n) | O(1) | General case, optimal solution |
Video Solution
Leetcode 4006: Count Valid Prefixes | Leetcode Biweekly Contest 188 • AlgorithmsWithJT • 5 views views
Watch 1 more video solutions →Frequently Asked Questions
Is Count Valid Prefixes easy or hard?
Count Valid Prefixes Python/Java solution
How to solve Count Valid Prefixes in O(n)?
What is the best approach for Count Valid Prefixes?
Is Count Valid Prefixes asked at Google/Amazon/Meta?
What data structure is used in Count Valid Prefixes?
What is the time complexity of Count Valid Prefixes?
Ready to solve this problem?
Practice Count Valid Prefixes with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor