题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
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 let arr = []; while(line = await readline()){ arr.push(line.split('')); } let key = Array.from(new Set(arr[0])); let a_charcode = 'a'.charCodeAt(); // 题目中写了,输入只有小写字母,所以一定要仔细看题,不需要考虑大写字母 for(let i=0;i<26;i++){ // 生成新的密码表 let c = String.fromCharCode(a_charcode+i); if(key.indexOf(c) == -1){ key.push(c); } } let ns = arr[1]; let mihou = ''; // 加密串 for(let j=0;j<ns.length;j++){ let c = ns[j].charCodeAt(); // 获取字符 charcode let index = c-a_charcode; // 获取字符在 abcd... 这种正常字母表 中原来的index mihou+=key[index]; // 根据index获得在新字母表中的字符,并加入加密串 } console.log(mihou); }()