题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int num = in.nextInt(); String[] inputWord = new String[num]; for (int i = 0; i < num ; i ++) { inputWord[i] = in.next(); } // 排序后的输入下标 String flagWord = in.next(); int k = in.nextInt(); List<String> copyWord = new ArrayList<>(); // 兄弟单词个数 int t = 0; for (int i = 0; i < num ; i ++) { if (inputWord[i].length() == flagWord.length() && !inputWord[i].equals(flagWord)) { // 首先单词长度一定要相同 char[] flagtmpchar = flagWord.toCharArray(); char[] tmpchar = inputWord[i].toCharArray(); boolean exFlag = true; // 单词是否互为兄弟单词 默认为是 for (int j = 0 ; j < flagtmpchar.length ; j ++) { boolean chek = false; exFlag = true; for (int m = 0 ; m < tmpchar.length ; m ++) { if (flagtmpchar[j] == tmpchar[m]) { tmpchar[m] = '0'; chek = true; break; } } if (!chek) { exFlag = false; break; } } // if (exFlag) { copyWord.add(inputWord[i]); t++; } } } System.out.println(copyWord.size()); if(k <= copyWord.size() && k > 0){ Collections.sort(copyWord); System.out.println(copyWord.get(k-1)); } } }