题解 | #字符串加密#
字符串加密
http://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
let line;
let inputArr = [];
while ((line = readline())) {
inputArr.push(line);
}
let char = "abcdefghijklmnopqrstuvwxyz";
enpry(inputArr[0], inputArr[1]);
function enpry(str1, str2) {
let arr = char.split("");
let input = new Set(str1.toLowerCase().split(""));
let inputArr = [...input];
for (let i of inputArr) {
if (arr.indexOf(i) != -1) {
arr.splice(arr.indexOf(i), 1);
}
}
// 计算参照字符顺序数组
let referStr = inputArr.concat(arr);
// 加密
let result = "";
for (let j of str2) {
if (/[A-Z]/g.test(j)) {
let index = char.indexOf(j.toLowerCase());
result += referStr[indedx].toUpperCase();
} else {
let index = char.indexOf(j);
result += referStr[index];
}
}
console.log(result);
}
let inputArr = [];
while ((line = readline())) {
inputArr.push(line);
}
let char = "abcdefghijklmnopqrstuvwxyz";
enpry(inputArr[0], inputArr[1]);
function enpry(str1, str2) {
let arr = char.split("");
let input = new Set(str1.toLowerCase().split(""));
let inputArr = [...input];
for (let i of inputArr) {
if (arr.indexOf(i) != -1) {
arr.splice(arr.indexOf(i), 1);
}
}
// 计算参照字符顺序数组
let referStr = inputArr.concat(arr);
// 加密
let result = "";
for (let j of str2) {
if (/[A-Z]/g.test(j)) {
let index = char.indexOf(j.toLowerCase());
result += referStr[indedx].toUpperCase();
} else {
let index = char.indexOf(j);
result += referStr[index];
}
}
console.log(result);
}