把目标单词取出并排序,然后分别对比词典里的单词(同样要进行字符排序),把符合条件的兄弟单词加入列表,经过排序输出即可。
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String strs[] = str.split(" ");
int len = Integer.parseInt(strs[0]);
String words[] = new String[len];
int i=1;
for(;i<=len;++i)
words[i-1] = strs[i];
String target = strs[i++];
int order = Integer.parseInt(strs[i]);
char[]chs = target.toCharArray();
Arrays.sort(chs);
String sorttarget = new String(chs);
List<String> list = new ArrayList<>();
for(int j = 1;j<=len;++j){
if(!strs[j].equals(target)&&judge(strs[j],sorttarget))
list.add(strs[j]);
}
Collections.sort(list,new Comparator<String>(){
public int compare(String s1,String s2){
return s1.compareTo(s2);
}
});
if(order<list.size())
System.out.print(list.size()+"\n"+list.get(order-1));
else System.out.println(list.size());
}
public static boolean judge(String s,String target){
char chs [] = s.toCharArray();
Arrays.sort(chs);
if(new String(chs).equals(target))return true;
return false;
}
}