题解 | #字符串加密#
字符串加密
http://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
java LinkedHashSet
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()) {
String key = sc.nextLine();
String msg = sc.nextLine();
int length = key.length();
Set<Character> set = new LinkedHashSet<Character>();
for (int i = 0; i < key.length(); i++) {
set.add(key.charAt(i));
}
char start = 'a';
for (int i = 0; i < 26; i++) {
set.add(start);
start += 1;
}
ArrayList<Character> list = new ArrayList<>(set);
StringBuilder sb = new StringBuilder();
char[] arr = msg.toCharArray();
for (int i=0; i< arr.length; i++) {
if (arr[i] >= 'A' && arr[i] <= 'Z') {
int tmp = arr[i] - 'A';
sb.append(String.valueOf(list.get(tmp)).toUpperCase());
} else {
int tmp = arr[i] - 'a';
sb.append(String.valueOf(list.get(tmp)).toLowerCase());
}
}
System.out.println(sb.toString());
}
}
}