题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n=in.nextInt(); String[] arr=new String[n]; ArrayList<String> list=new ArrayList<String>(); for(int i=0;i<n;i++){ arr[i]=in.next(); } String str=in.next(); int k=in.nextInt(); int res=0; for(int i=0;i<n;i++){ if(isBrother(str,arr[i])){ res++; list.add(arr[i]); } } Collections.sort(list); System.out.println(list.size()); if(k<=list.size()){ System.out.println(list.get(k-1)); } } public static boolean isBrother(String s1,String s2){ if(s1.length()!=s2.length())return false; if(s1.equals(s2))return false; int len=s1.length(); HashMap<Character,Integer> m1=new HashMap<Character,Integer>(); HashMap<Character,Integer> m2=new HashMap<Character,Integer>(); for(int i=0;i<len;i++){ char c1=s1.charAt(i); char c2=s2.charAt(i); m1.put(c1,m1.getOrDefault(c1,0)+1); m2.put(c2,m2.getOrDefault(c2,0)+1); } // System.out.println(m1.toString()); if(m1.size()!=m2.size())return false; for(char c:m1.keySet()){ if(m1.get(c)!=m2.get(c))return false; } return true; } }