Problems > Balanced Prefix

Balanced Prefix

Difficulty: 1200 3000 ms 128 MB

strings prefix-sums

Print the length of the longest PREFIX containing equally many 0s and 1s (0 if none).

Input: one line with a binary string.

Output: the longest balanced prefix length.

Constraints:

  • 1 ≤ |S| ≤ 105

Sample Tests

Input 1
110100
Output 1
6
Input 2
111
Output 2
0

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.