题解 | #查找兄弟单词#

查找兄弟单词

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<String> arrayList = new ArrayList<>();
        ArrayList<String> arrayList1 = new ArrayList<>();
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            arrayList.add(sc.next());
        }
        String str = sc.next();
        int index = sc.nextInt();
        int count = 0;
        for (String s : arrayList) {
            if (isbrother(s, str)) {
                count++;
                arrayList1.add(s);
            }
        }
        Collections.sort(arrayList1);
        System.out.println(count);
        if(index>arrayList1.size()){
            System.out.println();
        }else 
        System.out.println(arrayList1.get(index-1));
    }
    public static boolean isbrother(String str1, String str2) {
        if (str1.length() != str2.length() || str1.equals(str2)) {
            return false;
        }
        char[] s1 = str1.toCharArray();
        char[] s2 = str2.toCharArray();
        Arrays.sort(s1);
        Arrays.sort(s2);
        return new String(s1).equals(new String(s2));
    }
}

全部评论

相关推荐

强大的马里奥:不太可能,我校计算机硕士就业率99%
点赞 评论 收藏
分享
醉蟀:你不干有的是人干
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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