题解 | #字符串加密#
字符串加密
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 line1 = in.nextLine(); String line2 = in.nextLine(); Set<String> keySet = new LinkedHashSet(); for(String s : line1.split("")) { keySet.add(s); } String key = String.join("",keySet); // System.out.println("key==:"+key); String az = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for(String s : az.split("")) { if(!key.toUpperCase().contains(s.toUpperCase())) { key += s; } } // key = key + az.substring(key.length()); // System.out.println("key==:"+key); for(int i=0;i<line2.length();i++) { String s = line2.substring(i,i+1); if(!s.matches("[a-zA-Z]")) { System.out.println(s); continue; } int index = az.indexOf(s.toUpperCase()); String replace = key.substring(index,index+1); replace = Character.isUpperCase(s.charAt(0)) ? replace.toUpperCase() : replace.toLowerCase(); System.out.print(replace); } } }