题解 | #查找兄弟单词#

查找兄弟单词

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

import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static ArrayList bro(String[] arry, String part) {
        ArrayList<String> lc = new ArrayList<>();
        for (int i = 0; i < arry.length; i++) {
            //-------选定一个arry[]与part比较
            ArrayList<String> la = new ArrayList<>();
            ArrayList<String> lb = new  ArrayList<>();
            if (arry[i].length() == part.length() && !arry[i].equals(part)) {
                for (int j = 0; j < part.length(); j++) {
                    la.add(String.valueOf(part.charAt(j)));//part是由哪些字符构成的
                    lb.add(String.valueOf(arry[i].charAt(j)));

                }
                Collections.sort(la);
                Collections.sort(lb);
                if (la.equals(lb)) {
                    lc.add(arry[i]);

                }

            }
        }
        return lc;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int wordnum = sc.nextInt();
        String[] wordinput = new String[wordnum];
        for (int i = 0; i < wordinput.length ; i++) {
            wordinput[i] = sc.next();
        }
        String part = sc.next();
        ArrayList<String> fin = bro(wordinput, part);
        System.out.println(fin.size());
        //-----------------------------------
        int item = 1;
        int num = sc.nextInt();
        Collections.sort(fin);
        for (String s : fin) {
//            if(item==num){
//                System.out.println(s);
//            }
            if (item == num) {
                System.out.println(s);
            }

            item++;
        }
    }
}

全部评论

相关推荐

06-17 21:57
门头沟学院 Java
白友:噗嗤,我发现有些人事就爱发这些,明明已读不回就行了,就是要恶心人
点赞 评论 收藏
分享
06-19 19:06
门头沟学院 Java
码农索隆:别去东软,真学不到东西,真事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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