# Beautiful Binary String – HackerRank Solution

In this post, we will solve Beautiful Binary String HackerRank Solution. This problem (Beautiful Binary String) is a part of HackerRank Problem Solving series.

Contents

Alice has aÂ binary string. She thinks a binary string is beautiful if and only if it doesn’t contain theÂ substringÂ ”010”.

In one step, Alice can change aÂ 0Â to aÂ 1Â or vice versa. Count and print the minimum number of steps needed to make Alice see the string as beautiful.

Example

b = 010

She can change any one element and have a beautiful string.

Function Description

Complete the beautifulBinaryString function in the editor below.

beautifulBinaryString has the following parameter(s):

• string b: a string of binary digits

Returns

• int:Â the minimum moves required

## Input Format

The first line contains an integerÂ n, the length of binary string.
The second line contains a single binary stringÂ b.

## Constraints

• 1 <= n <= 100
• b[i] âˆˆ {0, 1}

## Output Format

Print the minimum number of steps needed to make the string beautiful.

Sample Input 0

``````STDIN       Function
-----       --------
7           length of string n = 7
0101010     b = '0101010'``````

Sample Output 0

``2  ``

Explanation 0

In this sample,Â b = “0101010”

The figure below shows a way to get rid of each instance ofÂ “010”:

Make the string beautiful by changingÂ 2Â characters (b[2]Â andÂ b[5]).

Sample Input 1

``````5
01100``````

Sample Output 1

``0``

Explanation 1

The substringÂ “010”Â does not occur inÂ b, so the string is already beautiful inÂ 0Â moves.

Sample Input 2

``````10
0100101010``````

Sample Output 2

``3``

Explanation 2

In this sampleÂ b = “0100101010”

One solution is to change the values ofÂ b[2], b[5], and b[9]Â to form a beautiful string.

## Solution – Beautiful Binary String – HackerRank Solution

### C++

```#include<bits/stdc++.h>
using namespace std;
int main(){
int n,c=0;
string a;
cin>>n;
cin>>a;
int i=0;
while(i<a.length()){
if(a[i]=='0'&& a[i+1]=='1'&& a[i+2]=='0'){
c++;
i+=3;
}
else{
i+=1;
}
}
cout<<c<<endl;
}
```

### Python

```#!/bin/python3

import sys

def beautifulBinaryString(b):
res = 0

while '010' in b:
#print("found res = {} b = {}".format(res, b))
res += 1
b = b.replace("010","011", 1)

return res

if __name__ == "__main__":
n = int(input().strip())
b = input().strip()
result = beautifulBinaryString(b)
print(result)
```

### Java

```import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner input = new Scanner(System.in);
int n = input.nextInt();
input.nextLine();
String s = input.nextLine();
int switches = 0;

for(int i = 0; i < s.length()-2; i++)
{
if(s.charAt(i) == '0' && s.charAt(i+1) == '1' && s.charAt(i+2) == '0')
{
switches++;
i += 2;
}
}
System.out.println(switches);
}
}
```

Note: This problem (Beautiful Binary String) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.