Skip to content

Instantly share code, notes, and snippets.

@MariaSzubski
Last active July 21, 2016 22:25
Show Gist options
  • Select an option

  • Save MariaSzubski/558b37140d8e0371da7accac0f31d4a8 to your computer and use it in GitHub Desktop.

Select an option

Save MariaSzubski/558b37140d8e0371da7accac0f31d4a8 to your computer and use it in GitHub Desktop.
Swap a substring to remove '010' pattern. #hackerrank #strings
/*
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