题解 | #字符串加密#

字符串加密

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String a = in.nextLine().toLowerCase();
        String b = in.nextLine().toLowerCase();
        ArrayList<Character> wordsList = getWords(a);
        char[] words = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        HashMap<Character, Character> map = new HashMap<>();
        for (int i = 0; i < words.length; i++) {
            map.put(words[i], wordsList.get(i));
        }
        for (char ba : b.toCharArray()) {
            System.out.print(map.get(ba));
        }

    }

    public static ArrayList<Character> getWords(String a) {
        char[] c = a.toCharArray();
        HashSet set = new HashSet<Character>();
        ArrayList list = new ArrayList<Character>();
        for (char ca : c) {
            set.add(ca);
        }
        // String words="abcdefghijklmnopqrstuvwxyz";
        char[] words = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
        ArrayList<Character> wordList = new ArrayList<>();
        for (char word : words) {
            if (!set.contains(word)) {
                wordList.add(word);
            }

        }
        for (char ca : c) {
            if (set.contains(ca)) {
                list.add(ca);
                set.remove(ca);
            }
        }
        list.addAll(wordList);
        return list;
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务