题解 | #字符串排序#

字符串排序

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);
}

全部评论

相关推荐

01-19 12:48
门头沟学院 C++
只想搞钱的鸽子很喜欢...:混账是很多的,还有那些在自己风华正茂的年纪说风凉话讥讽那些下岗前员工的。这些人都是现在职场环境这么烂的帮凶
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务