题解 | #查找兄弟单词#

查找兄弟单词

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

全部评论

相关推荐

有工作后先养猫:太好了,是超时空战警,我们有救了😋
点赞 评论 收藏
分享
牛客963010790号:为什么还要收藏
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务