Lexicographically largest string possible by at most K replacements | JavaScript implementation of the above approach ; Traverse each element of the string ; If the current character can be replaced with ' z ' ; Return the modified string ; Driver code
function largestString ( s , k ) { for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] != ' ' && k > 0 ) { s = s . substring ( 0 , i ) + ' ' + s . substring ( i + 1 ) ; k -- ; } } return s ; } var s = " " ; var k = 1 ; document . write ( largestString ( s , k ) ) ;