题目链接
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d784c077b41e89d2de3bb62c3eeb17a1.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6882b72b340a653c50e215a0ccc12a2a.png)
一个小坑:题目中要求输出第x个兄弟单词。需要判断一下,如果这个数x太大了,就不输出。
package May16;
import java.util.*;
public class Demo11 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int i = in.nextInt();
String[] arr = new String[i];
for (int j = 0; j < i; j++) {
String str = in.next();
arr[j] = str;
}
Arrays.sort(arr);
String toFind = in.next();
int toFindInt = in.nextInt();
int total = 0;
LinkedList<String> list = new LinkedList<>();
for (int j = 0; j < i; j++) {
if (judge(toFind, arr[j])) {
total++;
list.add(arr[j]);
}
}
System.out.println(total);
if (toFindInt <= list.size())
System.out.println(list.get(toFindInt - 1));
}
}
public static boolean judge(String str1, String str2) {
if (str1 == null || str2 == null) return false;
if (str1.length() != str2.length()) {
return false;
}
if (str1.equals(str2)) return false;
char[] chars1 = str1.toCharArray();
char[] chars2 = str2.toCharArray();
Arrays.sort(chars1);
Arrays.sort(chars2);
for (int i = 0; i < chars1.length; i++) {
if (chars1[i] != chars2[i]) {
return false;
}
}
return true;
}
}