题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { String input = in.nextLine(); String[] inputArray = input.split(" "); int index = Integer.parseInt(inputArray[inputArray.length - 1]); int allSize=Integer.parseInt(inputArray[0]); String need = inputArray[inputArray.length - 2]; List<String> brotherList = new ArrayList<>(); for (int i = 1; i <= allSize; i++) { if (ifBrother(need, inputArray[i])) { brotherList.add(inputArray[i]); } } System.out.println(brotherList.size()); if (index <= brotherList.size()) { Collections.sort(brotherList); System.out.println(brotherList.get(index - 1)); } } } private static boolean ifBrother(String need, String inputArray) { if (need.length() != inputArray.length() || need.equals(inputArray)) { return false; } char[] chars1 = need.toCharArray(); char[] chars2 = inputArray.toCharArray(); Arrays.sort(chars1); Arrays.sort(chars2); return new String(chars1).equals(new String(chars2)); } }