题解 | #查找兄弟单词#
查找兄弟单词
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); ArrayList<String> arrayList = new ArrayList<>(); ArrayList<String> arrayList1 = new ArrayList<>(); int n = sc.nextInt(); for (int i = 0; i < n; i++) { arrayList.add(sc.next()); } String str = sc.next(); int index = sc.nextInt(); int count = 0; for (String s : arrayList) { if (isbrother(s, str)) { count++; arrayList1.add(s); } } Collections.sort(arrayList1); System.out.println(count); if(index>arrayList1.size()){ System.out.println(); }else System.out.println(arrayList1.get(index-1)); } public static boolean isbrother(String str1, String str2) { if (str1.length() != str2.length() || str1.equals(str2)) { return false; } char[] s1 = str1.toCharArray(); char[] s2 = str2.toCharArray(); Arrays.sort(s1); Arrays.sort(s2); return new String(s1).equals(new String(s2)); } }