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.
Input: S = “ancqz”, K = 2
Explanation: In one swap, we can swap characters ‘n’ and ‘q’ as they both are at even indices (2 and 4 assuming 1-based indexing). The string becomes “aqcnz”. In the second swap we can swap characters ‘a’ and ‘z’ as both have odd indices. The final string “zqcna” is the largest lexicographically possible using 2 swap operations.
Note: We cannot swap for instance ‘a’ and ‘n’ or ‘n’ and ‘z’ as one of them would be at an odd index while the other at even index.
Input: S = “geeksforgeeks”, K = 3
Naive approach: The naive approach is trivial. Use the greedy algorithm to make the current index maximum starting from left by picking the maximum possible character that is to the right of the current index and is also with the same parity of index i.e. (odd if the current index is odd and even if the current index is even). Repeat the same procedure atmost K times. The time complexity of the approach will be O(N2).
Efficient Approach: The above approach can be improved using a priority queue. Follow the steps below to solve the problem:
- Create two priority queue one for odd index characters and another for even index characters.
- Iterate over characters in the string, if the even index character comes then search the index which is greater than the current index and character bigger than the current character in the priority queue which is holding even characters. If there is any, swap the two characters push the current character and the index that we found in the priority queue.
- The same procedure is to be followed when the odd character comes.
- If K becomes 0, terminate the loop.
- The resultant string will be the answer.
Below is the implementation of the above approach:
Time Complexity: O(NlogN)
Auxiliary Space: O(N)