You are given a string SS, consisting of lowercase English letters. You can do the following operation at most once :
- Choose a subsequence of the string SS, then remove it from SS and concatenate the subsequence at the end of the remaining string.
Find the lexicographically minimum string that can be obtained by performing this operation at most once on SS.
- A string AA is a subsequence of a string BB if AA can be obtained by deleting several (possibly, zero or all) characters from BB.
- Given two strings of the same length AA and BB, we say that AA is lexicographically smaller than BB if there exists an index ii such that A1=B1,A2=B2,…,Ai−1=Bi−1A1=B1,A2=B2,…,Ai−1=Bi−1 and Ai<BiAi<Bi.
- The first line of input contains a single integer TT denoting the number of test cases. The description of TT test cases follows.
- The first and only line of each test case contains a string SS.
For each test case, print a single line containing the lexicographically minimum string which can be obtained from SS by performing the given operation at most once.
- SS contains only lowercase English letters.
- Sum of |S||S| over all test cases does not exceed 106106.
- Subtask 1 (100 points): Original constraints
Sample Input 1
4 aba abcd cbcdbef fabcdac
Sample Output 1
aab abcd bbccdef aacfbcd
Test case 11: It is optimal to choose the subsequence bb, then remove and concatenate the subsequence at the end of the remaining string aaaa. Hence the final string is aa+b=aabaa+b=aab.
Test case 22: The given string is already lexicographically minimum.
Test case 33: It is optimal to choose the subsequence ccdefccdef, then remove and concatenate the subsequence at the end of the remaining string bbbb. Hence the final string is bb+ccdef=bbccdefbb+ccdef=bbccdef.
Will be updated today.
Refer and Earn Rs. 1200 per referral on Upstox
Click here to know more
- Pyramid Traversal Solution | DEC LONG CHALLENGE | Codechef
- Increasing String Solution | DEC LONG CHALLENGE | Codechef
- Squares Counting Solution | DEC LONG CHALLENGE | Codechef
- Rock Paper Scissors Solution | DEC LONG CHALLENGE | Codechef
- Check Mate Solution | DEC LONG CHALLENGE | Codechef
Read More Post Here