DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Minimum Moves to Convert String


/**
 * @param {string} s
 * @return {number}
 */
var minimumMoves = function(s) {
   let move = 0;
    let i = 0;
    while(i<s.length){
        let char = s[i];
        // incrementing the index if we already have 'O'
        if(char== 'O'){
            i++;
        }
        // incrementing the move and index by 3 (Per move =  3 characters)
        if(char== 'X'){
            i=i+3;
            move++;
        }
    }
    return move;
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)