题解 | #查找兄弟单词#

查找兄弟单词

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String[] strs = new String[n];
        for (int i = 0; i < n; i++) {
            strs[i] = in.next();
        }
        String str = in.next();
        int index = in.nextInt();
        int count = 0;
        ArrayList list = new ArrayList();
        for (int i = 0; i < n; i++) {
            if (isBro(strs[i], str)) {
                count++;
                list.add(strs[i]);
            }
        }
        System.out.println(count);
        Collections.sort(list);
        if (count >= index) {
            System.out.println(list.get(index - 1));
        }
    }

    private static boolean isBro(String s, String str) {
        char[] cs = s.toCharArray();
        char[] chars = str.toCharArray();
        if (!s.equals(str)) {
            Arrays.sort(cs);
            Arrays.sort(chars);
            if (Arrays.equals(cs, chars)) return true;
            else return false;
        }
        return false;
    }
}

#java#
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-02 18:35
简历上把1个月实习写成了3个月,会进行背调吗?
码农索隆:一个月有一个月的实习经历,三个月有三个月的实习经历
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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