题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// 原字母表
const arr = ['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']
const newArr = []
let num = 0
rl.on('line', function (line) {
num++
if(num == 1){
// 新表头
const buff = line.split('')
for(let c of buff){
if(!newArr.includes(c)) newArr.push(c)
}
// 把原字母表的字母,没出现过在新表头的放进新字母表中
for(let c of arr){
if(!newArr.includes(c)) newArr.push(c)
}
}else if(num === 2) {
const cArr = line.split('')
let res = ''
for(let i = 0; i < cArr.length; i++){
// 在原字母表找到该字母的下标
const index = arr.findIndex((c)=>c === cArr[i])
// 从新表取出对应的字母
res += newArr[index]
}
console.log(res)
}
});

查看1道真题和解析