题解 | #字符串加密#

字符串加密

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

const readline = require('readline');

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


let [key, num, alphabet] = [undefined, 0, 'abcdefghijklmnopqrstuvwxyz']
rl.on('line', function (line) {
   num++
   if(num === 1){
    key = line
   }else if(num === 2){
        const res = []
        // 建立新字母表、我这里用不了扩展运算符,会报错,所以只能这么做,麻烦点
        const newAlphabet = arrRemoveDuplicates(key + alphabet) 
        // 然后根据明文的长度遍历,找对应字母在正常字母表的位置
        // 再从新字母表中相应位置截取字母,重复此步骤,最终会得到一个新的加密字符串
        for(let i = 0; i < line.length; i++){
            res.push(newAlphabet[alphabet.indexOf(line[i])])
        }

        console.log(res.join(''))
   }
});


function arrRemoveDuplicates(arr: any){
    const res = []
    for(let i = 0; i < arr.length; i++){
        if(!res.includes(arr[i])){
            res.push(arr[i])
        }  
    }
    return res
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务