题解 | #查找兄弟单词#

查找兄弟单词

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
         hj27();
    }

    /**
     * HJ27 查找兄弟单词
     */
    private static void hj27() {
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        List<String> lst = new ArrayList<>();
        while (num > 0) {
            lst.add(in.next());
            num--;
        }
        String target = in.next();
        char[] arr = target.toCharArray();
        Arrays.sort(arr);
        int index = in.nextInt();
        int count = 0;
        List<String> targetlst = new ArrayList<>();
        for (String str : lst) {
            char[] strArr = str.toCharArray();
            Arrays.sort(strArr);
            if (!target.equals(str) && Arrays.equals(arr, strArr)) {
                count++;
                targetlst.add(str);
            }
        }
        System.out.println(count);
        if (index <= count) {
            targetlst.sort(Comparator.naturalOrder());
            System.out.println(targetlst.get(index - 1));
        }

    }

}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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