Update appNew update is available. Click here to update.

Beautiful String

profile
Contributed by
Ankit Kharb
Easy
yellow-spark
0/40
18 mins
70 %
517 upvotes
Bank Of AmericaGojekAdobe
+3 more

Problem Statement

Ninja has been given a binary string β€˜STR’ containing either β€˜0’ or β€˜1’. A binary string is called beautiful if it contains alternating 0s and 1s.

For Example:β€˜0101’, β€˜1010’, β€˜101’, β€˜010’ are beautiful strings.

He wants to make β€˜STR’ beautiful by performing some operations on it. In one operation, Ninja can convert β€˜0’ into β€˜1’ or vice versa.

Your task is to determine the minimum number of operations Ninja should perform to make β€˜STR’ beautiful.

For Example :
Minimum operations to make β€˜STR’ β€˜0010’ beautiful is β€˜1’. In one operation, we can convert β€˜0’ at index β€˜0’ (0-based indexing) to β€˜1’. The β€˜STR’ now becomes β€˜1010’ which is a beautiful string. 
Detailed explanation ( Input/output format, Notes, Images )
Constraints :
1 <= T <= 100
2 <= |STR| <= 10^5
STR[i] = β€˜1’ or β€˜0’

Where '|STR|' denotes the length of β€˜STR’.

Time Limit: 1 sec 

Sample Input 1 :

2
0000
1010

Sample Output 1 :

2
0

Explanation of Sample Input 1 :

For the first test case:
The two beautiful strings that can be formed from the given β€˜STR’ 
are β€œ1010” and β€œ0101”. Ninja can transform β€˜STR’ to β€œ1010” by 
performing the following operations:
Replace β€˜0’ at index 0 by β€˜1’.
Replace β€˜0’ at index 2 by β€˜1’.

Ninja can transform β€˜STR’ to β€œ0101” by performing the following 
operations:
Replace β€˜0' at index 1 by β€˜1’.
Replace β€˜0’ at index 3 by β€˜1’.

The minimum number of operations in transforming β€˜STR’ to either of the two beautiful strings is 2.

For the second test case:
Given β€˜STR’ is already beautiful so the minimum number of operations required is 0.

Sample Input 2 :

2
01011
1001

Sample Output 2 :

1
2

Explanation of Sample Input 2 :

For the first test case:
The two beautiful strings that can be formed from the given β€˜STR’ are β€œ10101” and β€œ01010”. Ninja can transform β€˜STR’ to β€œ10101” by performing the following operations:
Replace β€˜0’ at index 0 by β€˜1’.
Replace β€˜1’ at index 1 by β€˜0’.
Replace β€˜0’ at index 2 by β€˜1’
Replace β€˜1’ at index 4 by β€˜0’.

Ninja can transform β€˜STR’ to β€œ01010” by performing the following operations:
Replace β€˜1’ at index 4 by β€˜0’.

The minimum number of operations in transforming β€˜STR’ to beautiful is the minimum of the above two which is 1.

For the second test case:
The two beautiful strings that can be formed from the given β€˜STR’ are β€œ1010” and β€œ0101”. Ninja can transform β€˜STR’ to β€œ1010” by performing the following operations:
Replace β€˜0’ at index 2 by β€˜1’.
Replace β€˜1’ at index 3 by β€˜0’.

Ninja can transform β€˜STR’ to β€œ0101” by performing the following operations:
Replace β€˜0’ at index 0 by β€˜1’.
Replace β€˜1’ at index 1 by β€˜0’.

The minimum number of operations in transforming β€˜STR’ to either of the two beautiful strings is 2.
Full screen
Reset Code
Full screen
Autocomplete
copy-code
Console