题解 | #查找兄弟单词#

查找兄弟单词

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

全部评论

相关推荐

10-17 12:16
同济大学 Java
7182oat:快快放弃了然后发给我,然后让我也泡他七天最后再拒掉,狠狠羞辱他一把😋
点赞 评论 收藏
分享
object3:开始给部分🌸孝子上人生第一课了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务