题解 | #字符串加密#
字符串加密
http://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
// 1.生成对照表
String strCom = sc.next();
ArrayList<Character> list = getComparison(strCom);
// 2.得到结果
String str = sc.next();
getResult(list, str);
}
}
private static void getResult(ArrayList<Character> listValue, String str) {
ArrayList<Character> listKey = new ArrayList<>();
HashMap<Character, Character> map = new HashMap<>();
for (char j = 'a'; j <= 'z'; j++) {
listKey.add(j);
}
for (int i = 0; i < listKey.size(); i++) {
map.put(listKey.get(i),listValue.get(i));
}
for (int n = 0; n < str.length(); n++) {
System.out.print(map.get(str.charAt(n)));
}
System.out.println();
}
public static ArrayList<Character> getComparison(String string) {
ArrayList<Character> list = new ArrayList<>();
for (int i = 0; i < string.length(); i++) {
if (list.contains(string.charAt(i))) {
continue;
} else {
list.add(string.charAt(i));
}
}
for (char j = 'a'; j <= 'z'; j++) {
if (list.contains(j)) {
continue;
} else {
list.add(j);
}
}
return list;
}
}