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.
Input: S = “baccc”
K = 2
Output: 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 = 3
Output: result = “zzxxxax”
- 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.
Time Complexity: O(N)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- K-th lexicographical string of given length
- Lexicographical Maximum substring of string
- Lexicographical concatenation of all substrings of a string
- Print all the combinations of a string in lexicographical order
- Print all lexicographical greater permutations of a given string
- Find the k-th string in lexicographical order consisting of n-2 X's and 2 Y's
- Largest lexicographic array with at-most K consecutive swaps
- Largest increasing subsequence of consecutive integers
- Find last two remaining elements after removing median of any 3 consecutive elements repeatedly
- Minimum number of elements to be removed so that pairwise consecutive elements are same
- Maximize the sum of differences of consecutive elements after removing exactly K elements
- Minimize the sum of differences of consecutive elements after removing exactly K elements
- Find elements of array using XOR of consecutive elements
- Replace array elements by sum of next two consecutive elements
- Average of remaining elements after removing K largest and K smallest elements from array
- Find Kth largest string from the permutations of the string with two characters
- Sum of consecutive two elements in a array
- Largest sub-string of a binary string divisible by 2
- Count consecutive pairs of same elements
- Find minimum sum such that one of every three consecutive elements is taken
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.