题解 | #字符串加密#

字符串加密

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 str_key = in.nextLine();
        String str_s = in.nextLine();
        in.close();

        char[] keys_1 = new char[]{'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'};
        char[] keys_2 = new char[26];
        int index = 0;

        Set<Character> set = new TreeSet<>();
        for(char c : str_key.toCharArray()){
            if(set.contains(c)){
                continue;
            }

            set.add(c);
            keys_2[index++] = c;
        }

        for(char c : keys_1){
            if(str_key.contains(c + "")){
                continue;
            }
            else{
                keys_2[index++] = c;
            }
        }

        StringBuilder res = new StringBuilder();
        for(char c : str_s.toCharArray()){
            if(Character.isLowerCase(c)){
                res.append(keys_2[c - 'a']);
            }
            else{
                char l_c = Character.toLowerCase(c);
                res.append(Character.toUpperCase(keys_2[c - 'a']));
            }
        }

        System.out.print(res.toString());
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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