Lexicographically largest string possible by at most K replacements Last Updated : 24 Jun, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string S of length N, consisting of lowercase alphabets, the task is to find the lexicographically longest string that can be obtained by replacing at most K characters from the given string. Examples: Input: S = "dbza", K = 1 Output: zbza Explanation: Replace S[0] (= 'd') with 'z' to obtain the lexicographically largest string. Input: S = "zzzz", K = 2 Output: zzzz Approach: The given problem can be solved by using the Greedy Approach. The idea is to traverse the array from left to right and replace all the non z characters with z. Follow the steps below to solve the problem: Run a loop for i = 0 to the length of the string:If the current character is not equal to z, and K is not equal to 0:Then, replace the current character with z.K = K - 1.Finally, return the modified string. Below is the implementation of the above approach: C++ // C++ implementation of // the above approach #include <bits/stdc++.h> using namespace std; string largestString(string s, int k) { // Traverse each element of the string for (int i = 0; i < s.size(); i++) { // If the current character // can be replaced with 'z' if (s[i] != 'z' && k > 0) { s[i] = 'z'; k--; } } // Return the modified string return s; } // Driver code int main() { string s = "dbza"; int k = 1; cout << largestString(s, k) << endl; return 0; } Java // Java implementation of the above approach class GFG{ static String largestString(String s, int k) { // Traverse each element of the string for(int i = 0; i < s.length(); i++) { // If the current character // can be replaced with 'z' if (s.charAt(i) != 'z' && k > 0) { s = s.replace(s.charAt(i),'z'); k--; } } // Return the modified string return s; } // Driver code public static void main(String args[]) { String s = "dbza"; int k = 1; System.out.println(largestString(s, k)); } } // This code is contributed by SoumikMondal Python3 # Python3 implementation of # the above approach def largestString(s, k): # Traverse each element of the string for i in range(len(s)): # If the current character # can be replaced with 'z' if (s[i] != 'z' and k > 0): s[i] = 'z' k -= 1 # Return the modified string return "".join(s) # Driver code if __name__ == '__main__': s = "dbza" k = 1 print (largestString([i for i in s], k)) # This code is contributed by mohit kumar 29 C# // C# implementation of // the above approach using System; using System.Collections.Generic; class GFG{ static string largestString(string s, int k) { // Traverse each element of the string for(int i = 0; i < s.Length; i++) { // If the current character // can be replaced with 'z' if (s[i] != 'z' && k > 0) { s = s.Replace(s[i],'z'); k--; } } // Return the modified string return s; } // Driver code public static void Main() { string s = "dbza"; int k = 1; Console.Write(largestString(s, k)); } } // This code is contributed by SURENDRA_GANGWAR JavaScript <script> // JavaScript implementation of // the above approach function largestString(s, k) { // Traverse each element of the string for (let i = 0; i < s.length; i++) { // If the current character // can be replaced with 'z' if (s[i] != 'z' && k > 0) { s = s.substring(0, i) + 'z' + s.substring(i + 1); k--; } } // Return the modified string return s; } // Driver code var s = "dbza"; var k = 1; document.write(largestString(s, k)); // This code is contributed by Potta Lokesh </script> Output: zbza Time Complexity: O(N) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Lexicographically largest string possible in one swap L lostsoul27 Follow Improve Article Tags : Strings Greedy DSA lexicographic-ordering Practice Tags : GreedyStrings Similar Reads Lexicographically largest possible String after removal of K characters Given a string S consisting of only lowercase letters, the task is to find the lexicographically largest string that can be obtained by removing K characters from the given string. Examples: Input: s = "zyxedcba", K=1 Output: zyxedcb Explanation: The character with the smallest ASCII value from the 5 min read Lexicographically largest string possible in one swap Given string str of length N, the task is to obtain the lexicographically largest string by at most one swap. Note: The swapping characters might not be adjacent. Examples: Input: str = "string" Output: tsring Explanation: Lexicographically largest string obtained by swapping string -> tsring. In 7 min read Lexicographically largest string after k removals Given a string s and an integer k, find the lexicographically largest string that can be obtained by removing exactly k characters from s, while preserving the relative order of the remaining characters.Examples:Input: s = "zebra", k = 3Output: "zr"Explanation: Removing "e", "b", and "a" results in 8 min read Lexicographically smallest string with period K possible by replacing '?'s from a given string Given a string S consisting of N lowercase characters and character '?' and a positive integer K, the task is to replace each character '?' with some lowercase alphabets such that the given string becomes a period of K. If it is not possible to do so, then print "-1". A string is said to be a period 9 min read Lexicographically largest string using at most K swaps at same parity indices Given string S and a positive integer K, the task is to find lexicographically the largest possible string using at most K swaps with the condition that the indices that are swapped must be either both odd or both even. Examples: Input: S = "ancqz", K = 2Output: "zqcna"Explanation: In one swap, we c 8 min read Lexicographically largest string possible consisting of at most K consecutive similar characters Given a string S and an integer K, the task is to generate lexicographically the largest string possible from the given string, by removing characters also, that consist of at most K consecutive similar characters. Examples: Input: S = âbacccâ, K = 2Output: ccbca Input: S = âccbbbâ, K = 2Output: ccb 8 min read Like