题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String str = in.nextLine();
String res = in.nextLine();
//存放新顺序的26个字母
char [] arr = new char[26];
//检验字母重复
int [] check = new int [26];
int j = 0;
//将key去重放入 arr中
for (int i = 0 ; i < str.length(); i++) {
char c = str.charAt(i);
if (check[c - 'a'] == 0) {
arr[j] = c;
j++;
}
check[c - 'a'] = 1;
}
//将剩余的字母按顺序放入arr中
for(int i = 0 ; i <26 ; i++){
if(check[i]==0){
arr[j] = (char)('a'+i);
j++;
}
}
StringBuffer resBuffer = new StringBuffer();
//解密
for(int i = 0 ; i <res.length();i++){
char c = arr[res.charAt(i)-'a'];
resBuffer.append(c);
}
System.out.println(resBuffer);
}
}
}
