题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function (line) {
const result = Array.from(line)
const arr = Array.from(line).filter((_:string)=>/[a-zA-Z]/g.test(_))
arr.sort((a:string,b:string)=>{
a = a.toLowerCase()
b = b.toLowerCase()
return a < b?-1:0
})
let cnt = 0
result.forEach((val:string, idx)=>{
if(/[a-zA-Z]/.test(val)){
result[idx] = arr[cnt]
cnt += 1
}
})
console.log(result.join(''))
});
思路:
如何解决各种奇怪的字符还在原位的问题。其实只需要做一个数组,然后保持这些字符位置不动。然后对剩下的字符排序。剩下的字符排好序后塞到数组里就好。
如何让保持相对位置不变,那就全部转换成小写比较
