题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String line = scan.nextLine(); String[] contnets = line.split(" "); int num = Integer.parseInt(contnets[0]); String brother = contnets[num + 1]; int last = Integer.parseInt(contnets[num + 2]); List<String> list = new ArrayList<>(num); String[] split = brother.split(""); for (int i = 1; i < num + 1; i++) { String content = contnets[i]; String co = content; if (split.length == content.length()) { boolean flag = true; for (int j = 0; j < split.length; j++) { // 不可重复 if (!co.contains(split[j])) { flag = false; } else { co = co.replaceFirst(split[j], ""); } } if (flag && !brother.equals(content)) { list.add(content); } } } List<String> collect = list.stream().sorted().collect(Collectors.toList()); System.out.println(collect.size()); if (0 < last && last <= collect.size()) { System.out.println(collect.get(last - 1)); } } }
#查找兄弟单词#