题解 | #查找兄弟单词#

查找兄弟单词

http://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.hasNext()) {
            int a = in.nextInt();
            String[] ss = new String[a];
            for(int i = 0; i<a; i++){
                ss[i] = in.next();
            }
            Arrays.sort(ss);
            String s = in.next();
            int b = in.nextInt();
            char[] cc = s.toCharArray();
            char[] cc1 = Arrays.copyOf(cc,cc.length);
            String[] newS = new String[ss.length];
            int cnt = 0;
            int index = 0;
            for(int i = 0; i<ss.length; i++){
                char[] ch = ss[i].toCharArray();
                if(cc.length != ch.length || Arrays.equals(cc,ch)){
                    continue;
                }else{
                    Arrays.sort(cc1);
                    Arrays.sort(ch);
                    if(!Arrays.equals(cc1,ch)){
                        continue;
                    }
                }
                newS[index++] = ss[i];
                cnt++;
            }
            String[] newSort = new String[cnt];
            System.arraycopy(newS,0,newSort,0,cnt);
            Arrays.sort(newSort);
            System.out.println(cnt);
            if(b>0 && b<=cnt){
                System.out.println(newSort[b-1]);
            }
        }
    }
}
全部评论

相关推荐

面试摇了我吧:啊哈哈面试提前五个小时发,点击不能参加就是放弃
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务