判断兄弟单词,我把两个字符串各自排序,排序后相等即是兄弟单词
要注意的是,题目是允许兄弟单词中有重复的,只是不允许和原来的单词重复。另外题目给的k是有可能超出你最后得到的兄弟单词的数量的,此时无需输出
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String[] s = in.nextLine().split(" ");
int n = Integer.parseInt(s[0]);
char[] target = s[n + 1].toCharArray();
Arrays.sort(target);
List<String> ts = new ArrayList<>();
for(int i = 1; i <= n; i++){
char[] t = s[i].toCharArray();
if(t.length != target.length || s[n + 1].equals(s[i]))
continue;
Arrays.sort(t);
int jud = 1;
for(int j = 0; j < t.length; j++){
if(target[j] != t[j]){
jud = 0;
break;
}
}
if(jud == 1)
ts.add(s[i]);
}
Collections.sort(ts);
System.out.println(ts.size());
int k = Integer.parseInt(s[n + 2]) - 1;
if(k < ts.size())
System.out.println(ts.get(k));
}
}