题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if(n<1 || n>1000) return; String[] arr = new String[n]; for(int i=0; i<n; i++){ String s= sc.next(); if(s.length()<1 || s.length()>10) continue; arr[i] = s; } Arrays.sort(arr); //字典序排序 String x = sc.next(); int k = sc.nextInt(); if(n<1 || k>n) return; int count = 0; List<String> list = new ArrayList<>(); for(String word: arr){ // if(list.contains(word)) continue; if(check(x, word)){ list.add(word); count++; } } System.out.println(list.size()); if(k <= list.size()){ // Collections.sort(list); System.out.println(list.get(k-1)); //下标从0开始 } } public static boolean check(String str, String bro){ if(str.equals(bro)) return false; if(str.length() != bro.length()) return false; /*for(int i=0; i<str.length(); i++){ if(!bro.contains(String.valueOf(str.charAt(i)))){ return false; } }*/ char[] s = str.toCharArray(); char[] b = bro.toCharArray(); Arrays.sort(s); Arrays.sort(b); return new String(s).equals(new String(b)); } }