题解 | #查找兄弟单词#

查找兄弟单词

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

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String input = in.nextLine();
            String[] inputArray = input.split(" ");
            int index = Integer.parseInt(inputArray[inputArray.length - 1]);
            int allSize=Integer.parseInt(inputArray[0]);
            String need = inputArray[inputArray.length - 2];
            List<String> brotherList = new ArrayList<>();
            for (int i = 1; i <= allSize; i++) {
                if (ifBrother(need, inputArray[i])) {
                    brotherList.add(inputArray[i]);
                }
            }
            System.out.println(brotherList.size());
            if (index <= brotherList.size()) {
                Collections.sort(brotherList);
                System.out.println(brotherList.get(index - 1));
            }

        }
    }

    private static boolean ifBrother(String need, String inputArray) {
        if (need.length() != inputArray.length() || need.equals(inputArray)) {
            return false;
        }
        char[] chars1 = need.toCharArray();
        char[] chars2 = inputArray.toCharArray();
        Arrays.sort(chars1);
        Arrays.sort(chars2);
        return new String(chars1).equals(new String(chars2));
    }
}

全部评论

相关推荐

头顶尖尖的程序员:我是26届的不太懂,25届不应该是找的正式工作吗?为什么还在找实习?大四还实习的话是为了能转正的的岗位吗
点赞 评论 收藏
分享
求offer的大角牛:不吃香菜
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-02 18:35
简历上把1个月实习写成了3个月,会进行背调吗?
码农索隆:一个月有一个月的实习经历,三个月有三个月的实习经历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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