题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String line = sc.nextLine(); String[] re = line.split("\\s"); int num = Integer.valueOf(re[0]); String word = re[num+1]; int find = Integer.valueOf(re[num+2]); List<String> set = new ArrayList<String>(); for(int i=1;i<num+1;i++){ if(IsBrother(re[i],word)) set.add(re[i]); } System.out.println(set.size()); if(set.size()!=0){ // Arrays.sort(set); Collections.sort(set); if(find<set.size()) System.out.println(set.get(find-1)); } } private static boolean IsBrother(String bro,String word){ if(bro.length() != word.length()) return false; if(bro.equals(word)) return false; char[] b = bro.toCharArray(); char[] w = word.toCharArray(); Arrays.sort(b); Arrays.sort(w); if(!Arrays.equals(b,w)) return false; return true; } }