1、判断是否是兄弟单词 2、找到所有的兄弟单词,并排序 3、如果k小于等于单词的总数,则输出字典顺序的第k个,数组索引k-1;否则不需要输出
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextLine()) { // 注意 while 处理多个 case
String[] map = in.nextLine().split("\\s+");
int len = map.length;
int num = Integer.parseInt(map[0]);//待查找的个数
int k = Integer.parseInt(map[len-1]);
String x = map[len-2];
int count = 0;
ArrayList<String> list = new ArrayList<>();
for(int i=1;i<=num;i++){
if(isBrotherWord(map[i],x)){
count++;
list.add(map[i]);
}
}
String[] res = new String[list.size()];
int index = 0;
for(String s:list){
res[index++] = s;
}
Arrays.sort(res);
System.out.println(count);
if(k<=count){//要查找的k小于等于总数count
System.out.println(res[k-1]);//第K个的索引是k-1
}
}
}
public static boolean isBrotherWord(String s1,String s2){
if(s1.length() != s2.length()){
return false;
}else if(s1.equals(s2)){
return false;
}else{
char[] cha1 = s1.toCharArray();
Arrays.sort(cha1);
char[] cha2 = s2.toCharArray();
Arrays.sort(cha2);
if(!new String(cha1).equals(new String(cha2))){
return false;
}else{
return true;
}
}
}
}