题解 | #查找兄弟单词#

查找兄弟单词

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

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        String[] strs = str.split(" ");
        int n = Integer.valueOf(strs[0]);
        int k = Integer.valueOf(strs[strs.length - 1]);
        String[] letters = new String[strs.length - 2];
        for (int i = 1, j = 0; i < strs.length - 1; i++, j++) {
            letters[j] = strs[i];
        }
        List<String> brothers = new ArrayList<>();
        for (int i = 0; i < letters.length - 1; i++) {
            String target = letters[letters.length - 1];
            if (target.length() == letters[i].length() && target.equals(letters[i])){
                continue;
            }
            char[] sources = letters[i].toCharArray();
            char[] targets = target.toCharArray();
            Arrays.sort(sources);
            Arrays.sort(targets);

            if (letters[i].length() == target.length() && new String(sources).equals(new String(targets))) {
                brothers.add(letters[i]);
            }
        }
        System.out.println(brothers.size());
        Object[] res = brothers.toArray();
        Arrays.sort(res);
        if(k <= res.length){
            System.out.println(res[k - 1]);
        }
        
    }
}

全部评论

相关推荐

轻絵梨花泪沾衣:南泵,大少爷驾到通通闪开
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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