题解 | #字符串排序#

字符串排序

https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
    // Write your code here
    while ((line = await readline())) {
        const allChars = [];
        for (const c of line) {
            if (/[a-zA-Z]/.test(c)) {
                allChars.push(c);
            }
        }

        allChars.sort(
            (a, b) =>
                a.toLowerCase().charCodeAt(0) - b.toLowerCase().charCodeAt(0)
        );

        let index = 0
        const results = []
        for (let i = 0; i < line.length; i++) {
            if (/[a-zA-Z]/.test(line[i])) {
                results[i] = allChars[index]
                index ++
            } else {
                results[i] = line[i]
            }
        }
        console.log(results.join(""))
    }
})();

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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