题解 | #查找兄弟单词#

查找兄弟单词

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

全部评论

相关推荐

03-26 13:44
南华大学 Java
在看面经的花生米很野蛮:这种情况下你当然要回答,你也是吗!!!!我超喜欢他的XXXXX
点赞 评论 收藏
分享
05-14 20:34
门头沟学院 Java
窝补药贝八股:管他们,乱说,反正又不去,直接说680
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务