Infosys Off Campus (SP and DSE) | OA | 2022
Anonymous User
512

Flip or Swap

You are given a binary string S of size N. In one operation you can perform either of the following:

  • You can swap any two characters of S.
  • You flip (0 to 1 or 1 to 0) for any character of S.

A valid string is any string where any two consecutive characters are not the same.
Find the minimum number of operations on S such that S becomes a valid string.
Constraints

1 ≤ N ≤ 10^5
1 ≤ len(S) ≤ 10^5

Input:
The first line contains an integer, N length of string.
The next line contains a string, S.

Test Case 1:

1
1

**Output:**
0

S="1", here already no two consecutive characters are same hence the answer is 0.

Test Case: 2

5
01101

**Output:**
1
S="01101"
If we swap the first and second character of the string we get S="10101" and we can see that here no two consecutive characters are the same.
Hence the answer is 1.

Test Case 3

5
00000

**Output:**
2
S="00000"
We can flip the 2nd and the 4th character of the string S.
S="01010"
Comments (1)