Given a string **S**, the task is to find the largest lexicographical string with no more than **K** consecutive occurrence of an element by either re-arranging or deleting the elements.

**Examples:**

Input:S = “baccc”

K = 2Output:Result = “ccbca”Explanation:Since K=2, a maximum of 2 same characters can be placed consecutively.

No. of ‘c’ = 3.

No. of ‘b’ = 1.

No. of ‘a’ = 1.

Since the largest lexicographical string has to be printed, therefore, the answer is “ccbca”.

Input:S = “xxxxzaz”

K = 3Output:result = “zzxxxax”

**Approach:**

- Form a frequency array of size 26, where index i is chosen using (character in string – ‘a’).
- Initialize an empty string to store corresponding changes.
- for i=25 to 0, do:
- If frequency at index i is greater than k, then append (i + ‘a’) K-times. Decrease frequency by K at index i.find the next greatest priority element and append to answer and decrease frequency at respective index by 1.
- If frequency at index i is greater than 0 but less than k, then append (i + ‘a’) times it’s frequency.
- If frequency at index i is 0, then that index cannot be used to form an element and therefore check for next possible highest priority element.

## C++

`// C++ code for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define ll long long int ` ` ` `// Function to find the ` `// largest lexicographical ` `// string with given constraints. ` `string getLargestString(string s, ` ` ` `ll k) ` `{ ` ` ` ` ` `// vector containing frequency ` ` ` `// of each character. ` ` ` `vector<` `int` `> frequency_array(26, 0); ` ` ` ` ` `// assigning frequency to ` ` ` `for` `(` `int` `i = 0; ` ` ` `i < s.length(); i++) { ` ` ` ` ` `frequency_array[s[i] - ` `'a'` `]++; ` ` ` `} ` ` ` ` ` `// empty string of string class type ` ` ` `string ans = ` `""` `; ` ` ` ` ` `// loop to iterate over ` ` ` `// maximum priority first. ` ` ` `for` `(` `int` `i = 25; i >= 0;) { ` ` ` ` ` `// if frequency is greater than ` ` ` `// or equal to k. ` ` ` `if` `(frequency_array[i] > k) { ` ` ` ` ` `// temporary variable to operate ` ` ` `// in-place of k. ` ` ` `int` `temp = k; ` ` ` `string st(1, i + ` `'a'` `); ` ` ` `while` `(temp > 0) { ` ` ` ` ` `// concatenating with the ` ` ` `// resultant string ans. ` ` ` `ans += st; ` ` ` `temp--; ` ` ` `} ` ` ` ` ` `frequency_array[i] -= k; ` ` ` ` ` `// handling k case by adjusting ` ` ` `// with just smaller priority ` ` ` `// element. ` ` ` `int` `j = i - 1; ` ` ` `while` `(frequency_array[j] ` ` ` `<= 0 ` ` ` `&& j >= 0) { ` ` ` `j--; ` ` ` `} ` ` ` ` ` `// condition to verify if index ` ` ` `// j does have frequency ` ` ` `// greater than 0; ` ` ` `if` `(frequency_array[j] > 0 ` ` ` `&& j >= 0) { ` ` ` `string str(1, j + ` `'a'` `); ` ` ` `ans += str; ` ` ` `frequency_array[j] -= 1; ` ` ` `} ` ` ` `else` `{ ` ` ` ` ` `// if no such element is found ` ` ` `// than string can not be ` ` ` `// processed further. ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// if frequency is greater than 0 ` ` ` `// and less than k. ` ` ` `else` `if` `(frequency_array[i] > 0) { ` ` ` ` ` `// here we don't need to fix K ` ` ` `// consecutive element criteria. ` ` ` `int` `temp = frequency_array[i]; ` ` ` `frequency_array[i] -= temp; ` ` ` `string st(1, i + ` `'a'` `); ` ` ` `while` `(temp > 0) { ` ` ` `ans += st; ` ` ` `temp--; ` ` ` `} ` ` ` `} ` ` ` ` ` `// otherwise check for next ` ` ` `// possible element. ` ` ` `else` `{ ` ` ` `i--; ` ` ` `} ` ` ` `} ` ` ` `return` `ans; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `string S = ` `"xxxxzza"` `; ` ` ` `int` `k = 3; ` ` ` `cout << getLargestString(S, k) ` ` ` `<< endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

zzxxxax

**Time Complexity:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Lexicographical smallest number after at most K consecutive swaps
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Largest lexicographic array with at-most K consecutive swaps
- Lexicographical concatenation of all substrings of a string
- Lexicographical Maximum substring of string
- K-th lexicographical string of given length
- Print all the combinations of a string in lexicographical order
- Find the k-th string in lexicographical order consisting of n-2 X's and 2 Y's
- Print all lexicographical greater permutations of a given string
- Length of longest consecutive ones by at most one swap in a Binary String
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Print all longest common sub-sequences in lexicographical order
- Sort the words in lexicographical order in Python
- Find the lexicographical next balanced bracket sequence
- Print all distinct circular strings of length M in lexicographical order
- Lexicographical smallest alternate Array
- Lexicographical ordering using Heap Sort
- Generate all numbers up to N in Lexicographical Order
- Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value K
- Lexicographically smallest array after at-most K consecutive swaps

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.