rambless

查找兄弟单词

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            List<String> list = new ArrayList<>(n);
            for (int i = 0; i < n; i++) {
                list.add(in.next());
            }
            String boy = in.next();
            int k = in.nextInt();

            List<String> boys = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                String temp = list.get(i);
                if (temp.length() == boy.length() && !temp.equals(boy)) {
                    char[] arr = temp.toCharArray();
                    boolean flag = true;
                    for (int j = 0; j < arr.length; j++) {
                        int index = boy.indexOf(arr[j]);
                        if (index == -1) {
                            flag = false;
                            break;
                        } else {
                            String p1 = boy.replace(boy.substring(index, index + 1), "");
                            String p2 = temp.replace(temp.substring(j, j + 1), "");
                            if (p1.length() != p2.length()) {
                                flag = false;
                                break;
                            }
                        }
                    }
                    if (flag) {
                        boys.add(temp);
                    }
                }
            }
            Collections.sort(boys);
            System.out.println(boys.size());
            if (boys.size() >= k) {
                System.out.println(boys.get(k - 1));
            }
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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