Last active
July 21, 2016 22:25
-
-
Save MariaSzubski/558b37140d8e0371da7accac0f31d4a8 to your computer and use it in GitHub Desktop.
Swap a substring to remove '010' pattern. #hackerrank #strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Solution for HackerRank > Algorithms > Strings > Beautiful Binary String | |
| https://www.hackerrank.com/challenges/beautiful-binary-string | |
| */ | |
| function main(){ | |
| var B = '0100101010'; | |
| var stepCount = 0; | |
| // While the substring '010' exists, replace it with '011'. | |
| // New substring ending in '11' reduces the total amount of steps needed. | |
| while (B.indexOf('010') > -1){ | |
| B = B.replace('010','011'); | |
| stepCount += 1; | |
| } | |
| console.log(stepCount); | |
| } | |
| main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment