题解 | #查找兄弟单词#
查找兄弟单词
http://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); String[] words=new String[n]; for(int i=0;i<n;i++){ words[i]=sc.next(); } String target=sc.next(); int k=sc.nextInt(); List<String> list=new ArrayList<>(); for(String s:words){ if(isBro(s,target)) list.add(s); } Collections.sort(list); System.out.println(list.size()); if(list.size()>=k){ System.out.println(list.get(k-1)); } } public static boolean isBro(String cur,String t){ if(t.equals(cur)) return false; if(t.length()!=cur.length()) return false; char[] chars1=t.toCharArray(); char[] chars2=cur.toCharArray(); Arrays.sort(chars1); Arrays.sort(chars2); return new String(chars1).equals(new String(chars2)); } }