题解 | #字符串加密#
字符串加密
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("")); });