题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
import java.util.*;
/*
解题思路:首先确定要查找兄弟单词的单词,然后把该单词和其余单词
进行对比看是否符合要求,按照题意,我们可以分别对该单词和其余单
词排序,看看两者是否相等,如果相等,则符合条件,就可以加入list中,
还要先设置一个if条件,只有其余单词和该单词不相等且它们长度相等时才进行比较
*/
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
String[] s=in.nextLine().split(" ");
int n=Integer.parseInt(s[0]);
int k=Integer.parseInt(s[s.length-1]);
String str=s[s.length-2];
char[] res=str.toCharArray();
Arrays.sort(res);
String newStr=new String(res);
List<String> list=new ArrayList<>();
for(int i=1;i<s.length-2;i++){
if(!s[i].equals(str) && str.length()==s[i].length()){
char[] arr=s[i].toCharArray();
Arrays.sort(arr);
String newSort=new String(arr);
if(newSort.equals(newStr)){
list.add(s[i]);
}
}
}
System.out.println(list.size());
if(k<=list.size()){
Collections.sort(list);
System.out.println(list.get(k-1));
}
}
}
}
