题解 | #字符串排序#

字符串排序

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

全部评论

相关推荐

牛客246576843号:建议简历对点优化,想做HR专门列出HR实习,想做运营专门列出运营实习,并且对点写出项目经历以及数据,同时在个人总结上可以多凸出和岗位的匹配度
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务