Hackerrank - Beautiful Binary String Solution
1 min read

Hackerrank - Beautiful Binary String Solution

Hackerrank - Beautiful Binary String Solution

Alice has a binary string. She thinks a binary string is beautiful if and only if it doesn't contain the substring .

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

For example, if Alice's string is  she can change any one element and have a beautiful string.

Function Description

Complete the beautifulBinaryString function in the editor below. It should return an integer representing the minimum moves required.

beautifulBinaryString has the following parameter(s):

  • b: a string of binary digits

Input Format

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


  • .

Output Format

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

Sample Input 0


Sample Output 0


Explanation 0:

In this sample,

The figure below shows a way to get rid of each instance of :

Because we were able to make the string beautiful by changing  characters ( and ), we print .

Sample Input 1


Sample Output 1


Sample Case 1:

In this sample

Explanation 1

The substring  does not occur in , so the string is already beautiful and we print .

Sample Input 2


Sample Output 2


Explanation 2

In this sample

One solution is to change the values of  to form a beautiful string.

Solution in Python

def beautifulBinaryString(a):
    return a.replace("010","011").count("1")-a.count("1")

Enjoying these posts? Subscribe for more

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.