题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
import java.util.Arrays;
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String secret = in.nextLine();
String pwd = in.nextLine();
//初始化
boolean[] initArray = new boolean[26];
char[] chs1 = new char[26];
int i = 0;
for(char ch : secret.toLowerCase().toCharArray()){
int index = ch - 'a';
if(initArray[index]){
continue;
}
initArray[index] = true;
chs1[i++]=ch;
}
for(int j = 0; j< initArray.length;j++){
if(!initArray[j]){
chs1[i++] = (char)('a'+j);
}
}
StringBuilder result = new StringBuilder();
for(char ch : pwd.toCharArray()){
if(ch >= 'a' && ch <= 'z'){
int index = ch - 'a';
result.append(chs1[index]);
}else if(ch >= 'A' && ch <= 'Z'){
int distance = 'a' - 'A';
int index = ch - 'a' + distance;
char pwdChar = (char)(chs1[index]-distance);
result.append(pwdChar);
}else{
result.append(ch);
}
}
System.out.println(result.toString());
}
}
