兄弟单词判断步骤:
1、跳过长度不一和完全相同的单词。
2、将2个String变为char[]。
3、对char[]进行排序,然后转为2个String。
4、对比两个String是否完全相同
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String temp[] = in.nextLine().split(" ");
int n = Integer.parseInt(temp[0]);
int k = Integer.parseInt(temp[temp.length - 1]);
String x = temp[temp.length - 2];
List<String> list = new ArrayList<>();
for(int i=1;i<=n;i++){
if(isBrother(x,temp[i])){
list.add(temp[i]);
}
}
System.out.println(list.size());
if(list.size()>=k){
Collections.sort(list);
System.out.println(list.get(k-1));
}
}
public static boolean isBrother(String x,String str){
if(x.length()!=str.length()||x.equals(str)){
return false;
}
char[] a = x.toCharArray();
char[] b = str.toCharArray();
Arrays.sort(a);
Arrays.sort(b);
String temp1 = new String(a);
String temp2 = new String(b);
return temp1.equals(temp2);
}
}