MariaSzubski
7/21/2016 - 10:25 PM

Swap a substring to remove '010' pattern. #hackerrank #strings

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();