题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
import java.util.Scanner; import java.util.ArrayList; import java.util.List; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String key = in.next(); String str = in.next(); System.out.println(encode(key,str)); } } private static String encode(String key, String str) { List<Character> list = new ArrayList<>(); for (int i = 0; i < key.length(); i++) { if (!list.contains(key.charAt(i))){ list.add(key.charAt(i)); } } for (int i = 0; i < 26; i++) { char c = (char) ((char) i+97); if (!list.contains(c)){ list.add(c); } } StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < str.length(); i++) { stringBuilder.append(list.get(str.charAt(i)-97)); } return stringBuilder.toString(); } }