F - Exchange Gym - 101102F

Given a string of lowercase English letters. You are allowed to choose two letters that exist in any position in the string, replace all occurrences of the first letter you chose with the second one, and replace all occurrences of the second letter you chose with the first one.

Your task is to find the string that comes first in dictionary order among all possible strings that you can get by performing the above operation at most once.

For example, by exchanging letter ‘a’ with letter ‘h’ in string “hamza”, we can get string “ahmzh”.

Input
The first line of input contains a single integer T, the number of test cases.

Each test case contains a non-empty string on a single line that contains no more than 105 lowercase English letters.

Output
For each test case, print the required string on a single line.

Example
Input
3
hamza
racecar
mca
Output
ahmzh
arcecra
acm
题意:可以对字符串进行一个操作,把2个字母互换(这2个字母是不同的),一旦互换必须把所有x换成y,所有y换成x,最多可以进行一次这样的操作,求字典序最小的字符串。

以s1[i]记录第i个字符是否可以用来交换,以s2[i]记录第i个字符是否在之前已经出现过,对于第i个字符,如果s2[i]=1说明之前已经出现过这个字符,之前就没换现在肯定也不会换,如果s2[i]=0说明这个字符可能可以换,那么就找最小的j满足s1[j]=1表示第i个字符能够用来交换,那么交换s[i]和j+’a’即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值