Lexicographically largest string possible by at most K replacements | Java 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
class GFG { static String largestString ( String s , int k ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) != ' z ' && k > 0 ) { s = s . replace ( s . charAt ( i ) , ' z ' ) ; k -- ; } } return s ; } public static void main ( String args [ ] ) { String s = " dbza " ; int k = 1 ; System . out . println ( largestString ( s , k ) ) ; } }