Convert given Binary string S to all 1 s by changing all 0 s to 1 s in range [ i + 1 , i + K ] if S [ i ] is 1 | Function to check whether all 0 s in the string can be changed into 1 s ; Store the count of 0 s converted for the last occurrence of 1 ; Declere a stack ; Traverse the string , S ; If stack is empty ; There is no 1 that can change this 0 to 1 ; Push 1 into the stack ; The last 1 has reached its limit ; New 1 has been found which can now change at most K 0 s ; If flag is 1 , print " YES " else print " NO " ; Given Input ; Function call
function changeCharacters ( S , N , K ) { let flag = 1 ; let count = 0 ; let st = new Array ( ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( st . length == 0 ) { if ( S [ i ] == ' ' ) { flag = 0 ; break ; } count = 0 ; st . push ( S [ i ] ) ; } else { if ( S [ i ] == ' ' ) { count ++ ; if ( count == K ) { st . pop ( ) ; count = 0 ; } } else { count = 0 ; } } } if ( flag == 1 ) document . write ( " " ) ; else document . write ( " " ) ; } let S = " " ; let N = S . Length ; let K = 2 ; changeCharacters ( S , N , K ) ;