题解 | #查找兄弟单词#

查找兄弟单词

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

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            String[] str = new String[n];
            for(int i = 0; i < n; i++){
                str[i] = sc.next();
            }
            String x = sc.next();
            int k = sc.nextInt();
            int index = 0;
            int count = 0;

            List<String> res = new ArrayList<>();
            while(index < n){
                String s = str[index];
                if (s.length() == x.length() && !x.equals(s)){
                    char[] s_temp = s.toCharArray();
                    char[] x_temp = x.toCharArray();
                    Arrays.sort(s_temp);
                    Arrays.sort(x_temp);
                    if (Arrays.equals(s_temp, x_temp)){
                        for (int i = 0; i < s.length(); i++){
                            if (x.contains(s.substring(i, i))){
                                count++;
                            }else{
                                count = 0;
                            }
                            if (count == x.length()){
                                res.add(s);
                                count = 0;
                            }
                        }
                    }
                }
                index++;
            }
            Collections.sort(res);
            System.out.println(res.size());
            if(k > res.size()){
                break;
            }else{
                System.out.print(res.get(k - 1));
            }
        }
    }
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务