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.

Task

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.

Leave a Comment

Your email address will not be published. Required fields are marked *