题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
import { log } from "console"; import { on } from "events"; import { stdin, stdout } from "process"; import { createInterface } from "readline"; const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); rl.on("line", function (line) { solution(line); }); function solution(line: string) { let pairs = line .split("") .map((x, index) => [x, index] as [string, number]) .filter(([x, index]) => /[A-Za-z]/.test(x)); let positions = pairs.map(([x, index]) => index); let chars = pairs.map(([x, index]) => x); chars.sort( (a, b) => a.toLowerCase().charCodeAt(0) - b.toLowerCase().charCodeAt(0) ); let charArr = line.split(""); for (let index = 0; index < chars.length; index++) { const element = chars[index]; charArr[positions[index]] = element; } let res = charArr.join(""); console.log(res); }