Beautiful Binary String

Pratik Somwanshi
Hackerrank Algorithms
2 min readJul 31, 2019

--

Hello World!

Let’s understand the Beautiful Binary String problem on HackerRank. You may click on the title to read the problem statement. So let’s start.

Here, we have to eliminate all the ‘010’ sub strings from the given string. You can change any of the zeros to ones and ones to zeros. We have to achieve the task with minimum moves. Let’s learn which digits to change with an example.

Consider the string ‘010101’. There are three approaches to do this:

  1. Change 010 to 110
  2. Change 010 to 000
  3. Change 010 to 011

Let’s see the impact on out input string. Start scanning from left, with first method, it takes two moves. With second method, it takes two moves. With third method, it takes one move. Let’s see what happens if we start scanning from right. With first method, it takes one move. With second method, it takes two moves. With third method, it takes two moves.

So this makes things clear for us. While changing 010 to any form, make sure that you also try that it avoids subsequent 010 also. Code might vary with whether you start scanning the string from right or left.

Open for suggestions. The code for this problem in Python3 can be found here.

Happy coding…

--

--