题解 | #字符串加密#
字符串加密
http://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
JAVA
// ABCDEFGHIJKLMNOPQRSTUVWXYZ // nihaobcdefgjklmpgrstuvwxyz // ni import java.util.*; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ String inputStr =scanner.nextLine(); String keyStr =scanner.nextLine(); String letterStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char[] letterArr = letterStr.toCharArray(); StringBuilder sb = new StringBuilder(); StringBuilder firstSb = new StringBuilder(); StringBuilder outputSb = new StringBuilder(); //输入的字符串inputStr去重 for(int i =0; i<inputStr.length();i++){ if(!firstSb.toString().contains((inputStr.charAt(i)+"").toLowerCase())){ firstSb.append(inputStr.charAt(i)); } } for(int i =0; i<letterArr.length;i++){ if(!inputStr.toUpperCase().contains(letterArr[i]+"")){ firstSb.append(letterArr[i]); } } //得到用于做对比的字符串 inputStr = firstSb.toString(); //在拿keyStr和inputStr做匹配 for(int i =0; i<keyStr.length();i++){ for(int j =0; j<letterStr.length();j++){ if(Character.toUpperCase(keyStr.charAt(i)) == Character.toUpperCase(letterStr.charAt(j))){ //判断输入的keyStr是大小写的处理 if(Character.isUpperCase(keyStr.charAt(i))){ outputSb.append(inputStr.charAt(j)); }else{ outputSb.append((inputStr.charAt(j)+"").toLowerCase()); } } } } System.out.println(outputSb.toString()); } } }