Choose and Swap
Submissions: 1029   Accuracy:

13.87%

  Difficulty: Easy   Marks: 2

You are given a string s of lower case english alphabets. You can choose any two characters in the string and replace all the occurences of the first character with the second character and replace all the occurences of the second character with the first character. Your aim is to find the lexicographically smallest string that can be obtained by doing this operation at most once.

Input:
The first line of input contains an integer  T, denoting the number of test cases. Then T test cases follow. Each test case contains a string s of lower case English alphabets.

Output:
For each test case, in a new line print the lexicographically smallest string that is possible after doing the operation at most once.

Constraints:
1<=T<=300
1<=length of s<=10000

Example:
Input:
2
ccad
abba

Output:
aacd
abba

Explanation:
First Test Case
In ccad, we choose ‘a’ and ‘c’ and after doing the replacement operation once we get, aacd and this is the lexicographically smallest string possible.

Second Test Case
In abba, we can get baab after doing the replacement operation once for ‘a’ and ‘b’ but that is not lexicographically smaller than abba and so the answer is abba.

 

** For More Input/Output Examples Use 'Expected Output' option **

Author: Hemang Sarkar


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.