思路
- 对输入的字符串数组进行排序
- 扫描这个数组,排除掉长度不同和完全一样的字符串,得到一个新的列表;
- 判断这个列表中谁是兄弟
package huawei;
import java.util.*;
public class Main {
private static boolean isBrother(String a1, String a2) {
char[] chars1 = a1.toCharArray();
char[] chars2 = a2.toCharArray();
Arrays.sort(chars1);
Arrays.sort(chars2);
StringBuilder temp1 = new StringBuilder();
StringBuilder temp2 = new StringBuilder();
for (char c : chars1) {
temp1.append(String.valueOf(c));
}
for (char c : chars2) {
temp2.append(String.valueOf(c));
}
return temp1.toString().equals(temp2.toString());
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int num = in.nextInt();
List<String> words = new ArrayList<>();
for (int i = 0; i < num; i++) {
words.add(in.next());
}
words.sort(String::compareTo);
String target = in.next();
int index = in.nextInt();
List<String> temp = new ArrayList<>();
for (String word : words) {
if (!word.equals(target) && word.length() == target.length()) {
temp.add(word);
}
}
List<String> brothers = new ArrayList<>();
for (String s : temp) {
if (isBrother(s, target)) {
brothers.add(s);
}
}
System.out.println(brothers.size());
if (brothers.size() > index) {
System.out.println(brothers.get(index-1));
}
}
}
}