题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); char[] str1 = in.nextLine().toCharArray(); char[] str2 = in.nextLine().toCharArray(); HashMap<Character, Boolean> hashmap = new HashMap<>(); HashMap<Integer, Character> letterMap = new HashMap<>(); // 构造正常顺序表 for (char ch = 'a'; ch <= 'z'; ch++) { hashmap.put(ch, true); } // 构造新字母表 // {0:'a'} int i = 0; for (char ch : str1) { //先判断是否已经存储过 if (letterMap.containsValue(ch)) { continue; } // 加入字母表字典 letterMap.put(i,ch); // 将加入过的字母置于false hashmap.put(ch, false); i++; } // 补全新字母表 for (char ch = 'a'; ch <= 'z'; ch++) { if (hashmap.get(ch)) { letterMap.put(i,ch); i++; } } // 加密str2 for (char ch : str2) { // 找到正序子母表的索引 int index = ch - 'a'; // 用这个索引数值找到对应的字母 System.out.print(letterMap.get(index)); } } }