题解 | #字符串加密#

字符串加密

http://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

    while (sc.hasNext()) {
        char[] sig = sc.nextLine().toCharArray();
        char[] secret = sc.nextLine().toCharArray();
        Map<Character, Character> map = new HashMap<>();
        Set<Character> hash = new HashSet<>();
        char c_init = 'a';

        for (Character ch : sig) {
            if (!hash.contains(ch)) {
                hash.add(ch);
                map.put(c_init++, ch);
            }
        }

        for (char c = 'a'; c <= 'z'; c++) {
            if (!hash.contains(c)) {
                map.put(c_init++, c);
                hash.add(c);
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < secret.length; i++) {
            char ch = secret[i];
            if (Character.isUpperCase(ch)) {
                ch = Character.toLowerCase(ch);
                ch = Character.toUpperCase(map.get(ch));
            } else {
                ch = map.get(ch);
            }
            sb.append(ch);
        }
        System.out.println(sb.toString());
    }
}

}

全部评论

相关推荐

点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务