题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
String s = in.nextLine();
//将str中的字符添加到LinkedHashSet中,保证元素有序且不重复
LinkedHashSet<Character> set = new LinkedHashSet<>();
for(int i=0;i<str.length();i++){
set.add(str.charAt(i));
}
//将26个字母中剩下还没添加的添加进来
for(Character i='a';i<='z';i++){
set.add(i);
}
//转成数组
//arr:建立的小写字母的字母表
ArrayList<Character> arr = new ArrayList<>(set);
String res = "";
for(int i=0;i<s.length();i++){
int index = (int)(s.charAt(i)-'a');
res += arr.get(index);
}
System.out.println(res);
}
}
查看18道真题和解析