题解 | #查找兄弟单词#

查找兄弟单词

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));
    }
}

全部评论

相关推荐

05-19 15:21
已编辑
门头沟学院 Java
白火同学:你才沟通了200,说实话,北上广深杭这里面你连一座城市的互联网公司都没投满呢,更别说还有各种准一线二线城市了。等你沟通突破了三位数,还没结果再考虑转行的事吧。
点赞 评论 收藏
分享
Lorn的意义:你这种岗位在中国现在要么牛马天天加班,要么关系户进去好吃好喝,8年时间,真的天翻地覆了,对于资本来说你就说一头体力更好的牛马,哎,退伍没有包分配你真的亏了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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