就是这么暴力....
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
int n = in.nextInt();
String[] arr = new String[n];
// 3 abc bca cab abc 1
for (int i = 0; i < n; i++) {
arr[i] = in.next();
}
String x = in.next();
int k = in.nextInt();
String[] brotherWord = findBrother(x, arr);
System.out.println(brotherWord.length);
if (k - 1 < brotherWord.length) {
System.out.println(brotherWord[k - 1]);
}
}
}
private static String[] findBrother(String x, String[] arr) {
if (arr == null || arr.length == 0 || x == null) {
return new String[0];
}
List<String> temp = new ArrayList<>();
for (String s : arr) {
if (!s.equals(x) && isBrother(s, x)) {
temp.add(s);
}
}
temp.sort(String::compareTo);
temp.forEach(System.out::println);
String[] ret = new String[temp.size()];
temp.toArray(ret);
return ret;
}
private static boolean isBrother(String s, String x) {
if (s == null || x == null || s.length() != x.length()) {
return false;
}
for (char c : s.toCharArray()) {
if (!x.contains(c + "")) {
return false;
}
}
for (char c : x.toCharArray()) {
if (!s.contains(c + "")) {
return false;
}
}
// 取出每一个字母的次数 不一样就不是
Map<String, Integer> maps = new HashMap<>(s.length());
for (char c : s.toCharArray()) {
if (!maps.containsKey(c + "")) {
maps.put(c + "", 1);
} else {
maps.put(c + "", maps.get(c + "") + 1);
}
}
Map<String, Integer> mapx = new HashMap<>(x.length());
for (char c : x.toCharArray()) {
if (!mapx.containsKey(c + "")) {
mapx.put(c + "", 1);
} else {
mapx.put(c + "", mapx.get(c + "") + 1);
}
}
for (Map.Entry<String, Integer> entry:maps.entrySet()){
if(!mapx.containsKey(entry.getKey()) || mapx.get(entry.getKey()).intValue()!=entry.getValue().intValue()){
return false;
}
}
for (Map.Entry<String, Integer> entry : mapx.entrySet()) {
if(!maps.containsKey(entry.getKey()) || maps.get(entry.getKey()).intValue()!=entry.getValue().intValue()){
return false;
}
}
return true;
}