AmazonOA #116
Convert to Good String
Estimated MediumJudge-ready
Topics (AI-labelled)Prefix & RangesStrings
You are given a string s containing only 0 and 1. A string is called good if it contains neither the subsequence "010" nor the subsequence "101" (subsequences need not be contiguous). Each operation changes one 0 to 1 or one 1 to 0. Find the minimum number of operations needed to turn s into a good string.
Input
One line with a 01 string s. 1≤(length of s)≤10^6.
Output
Output one integer, the minimum number of operations.
Example 1
Input:
111101110100
Output:
2
Example 2
Input:
01010
Output:
2
Example 3
Input:
1100
Output:
0
Example notes
Example 1: s="111101110100"; changing the 0s at positions 5 and 9 (1-indexed) to 1 gives "111111111100", 2 operations in total. Example 2: "01010" can become "00000" or "01111", among others, needing at least 2 operations. Example 3: "1100" is already good, so the answer is 0.
Editorial
Loading solution…