题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
String str = br.readLine();
String[] arrs = str.trim().split(" ");
int count = Integer.valueOf(arrs[0]);
List<String> texts = new ArrayList<>();
for (int i = 1; i <= count; i++) {
texts.add(arrs[i]);
}
String word = arrs[count + 1];
int index = Integer.valueOf(arrs[arrs.length - 1]);
Iterator<String> iterator = texts.iterator();
String[] strings = word.split("");
Map<String, Integer> map = new HashMap<>();
for (String s : strings) {
Integer i = map.get(s);
if (i == null) {
map.put(s, 1);
} else {
map.put(s, i + 1);
}
}
while (iterator.hasNext()) {
String next = iterator.next();
if (word.equals(next) || word.length() != next.length()) {
iterator.remove();
continue;
}
boolean isNext = true;
String[] split = next.split("");
Map<String, Integer> tempMap = new HashMap<>();
for (String sp : split) {
if (map.get(sp) == null) {
iterator.remove();
isNext = false;
break;
}
Integer y = tempMap.get(sp);
if (y == null) {
tempMap.put(sp, 1);
} else {
tempMap.put(sp, y + 1);
}
}
if (!isNext) {
continue;
}
for (String sp : split) {
if (!tempMap.get(sp).equals(map.get(sp))) {
iterator.remove();
break;
}
}
}
Collections.sort(texts);
System.out.println(texts.size());
System.out.println(texts.get(index - 1));
} catch (Exception e) {
e.printStackTrace();
}
}
}

网易游戏公司福利 643人发布