题解 | #查找兄弟单词#

查找兄弟单词

https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String string = scanner.nextLine();
        String[] strArray = string.split(" ");
        int k = Integer.parseInt(strArray[strArray.length - 1]);
        String str = strArray[strArray.length - 2];
        List<String> lists = new ArrayList<>();
        List<String> lists2 = new ArrayList<>();

        for (int i = 1; i < strArray.length - 2; i++) {
            String tmp = strArray[i];
            lists.add(tmp);
        }
        for (String s : lists) {
            if (s.length() == str.length() && !s.equals(str)) {
                char[] chars = s.toCharArray();
                char[] chars1 = str.toCharArray();
                Arrays.sort(chars);
                Arrays.sort(chars1);
                if (Arrays.equals(chars, chars1)) {
                    lists2.add(s);
                }
            }
        }

        Collections.sort(lists2);
        System.out.println(lists2.size());
        if (lists2.size() > 0 && k >= 1 && k <= lists2.size()) {
            System.out.println(lists2.get(k - 1));
        }
    }
}

全部评论

相关推荐

牛客83265014...:完了,连现在都没开始面,13号投的是不是晚了
秋招的第一个offer,...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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