题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); let secreatWord = ""; rl.on("line", function (line) { if (!secreatWord) { secreatWord = line; } else { const charTable = "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"; let officialChars = charTable.split(" ").map((a) => a.toLowerCase()); let handledTable = secreatWord .split("") .filter((s, i, arr) => arr.indexOf(s) === i); // 需要去重 let targetIndexs = new Array(line.length); for (let i = 0; i < officialChars.length; i++) { const current = officialChars[i]; if (!handledTable.includes(current)) { handledTable.push(current); } for (let j = 0; j < line.length; j++) { if (line[j] === current) { targetIndexs[j] = i; } } } console.log( targetIndexs.reduce((acc, cur) => acc + handledTable[cur], "") ); } });