题解 | #字符串加密#

字符串加密

https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3

const readline = require("readline");

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});

const lines: string[] = [];
const alphabet = [
    "a",
    "b",
    "c",
    "d",
    "e",
    "f",
    "g",
    "h",
    "i",
    "j",
    "k",
    "l",
    "m",
    "n",
    "o",
    "p",
    "q",
    "r",
    "s",
    "t",
    "u",
    "v",
    "w",
    "x",
    "y",
    "z",
];

rl.on("line", function (line: string) {
    lines.push(line);
});

rl.on("close", () => {
    const encryptAlphabet = lines[0]
        .split("")
        .filter((item, index, self) => self.indexOf(item) === index);
    alphabet.forEach((item) => {
        if (
            !encryptAlphabet.includes(item) &&
            !encryptAlphabet.includes(item.toUpperCase())
        ) {
            encryptAlphabet.push(item);
        }
    });

    const encryptStr: string[] = [];
    for (let i = 0; i < lines[1].length; i++) {
        const index = alphabet.findIndex((item) => item === lines[1][i]);
        if (index > -1) {
            encryptStr.push(encryptAlphabet[index]);
        } else {
            encryptStr.push(lines[1][i]);
        }
    }

    console.log(encryptStr.join(""));
});

全部评论

相关推荐

如题,这操作。。。。
真烦好烦真烦:既想享受国家的招聘应届生福利,又不想培养新人,我只能说这种企业的ld太过分了
投递美的集团等公司6个岗位 >
点赞 评论 收藏
分享
吴offer选手:学到了,下次面试也放张纸在电脑上,不然老是忘记要说哪几个点
点赞 评论 收藏
分享
03-29 19:11
门头沟学院 Java
wyp_davis:是可以这样的,不过只要交钱就是假的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务