Lexicographically largest string possible by at most K replacements | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; string largestString ( string s , int k ) { for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] != ' z ' && k > 0 ) { s [ i ] = ' z ' ; k -- ; } } return s ; } int main ( ) { string s = " dbza " ; int k = 1 ; cout << largestString ( s , k ) << endl ; return 0 ; }