Talentd
FleetCode

4006. Count Valid Prefixes

Easy78.0% acceptance

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 <= 100
  • s consists only of '0' and '1'.

Hints open one at a time

Hint 1

A binary string can be rearranged into an alternating string if the numbers of '0' and '1' differ by at most one.

Hint 2

Scan s from left to right, maintain the counts of both characters in the current prefix, and count the prefixes satisfying this condition.

9:45
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def countValidPrefixes(self, s: str) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"00101"
Input
"101"
Your code is saved on this device.Solution