# 33. 字符串变换最小字符串
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on('line', function(line) {
const sortedArr = s.split('').sort();
if (sortedArr.join('') === s) {
console.log(s);
return;
}
let sb = s.split('');
for(let i=0; i<s.length; i++) {
if (s[i] !== sortedArr[i]) {
let swapIndex = -1;
for(let j = i+1; j<s.length; j++) {
if (sb[j] === sortedArr[i]) {
swapIndex = j;
}
}
[sb[i], sb[swapIndex]] = [sb[swapIndex], sb[i]];
break;
}
}
console.log(sb.join(''));
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30