题解 | #查找兄弟单词#
查找兄弟单词
http://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String[] strs = sc.nextLine().split(" ");
Integer n = Integer.parseInt(strs[0]);
String x = strs[strs.length - 2];
Integer k = Integer.parseInt(strs[strs.length - 1]);
List<String> letters = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (isBrother(x, strs[i])) {
letters.add(strs[i]);
}
}
int size = letters.size();
System.out.println(size);
if (size >= k) {
Collections.sort(letters);
System.out.println(letters.get(k - 1));
}
}
}
public static boolean isBrother (String s1, String s2) {
if (s1.length()!=s2.length() || s2.equals(s1)) {
return false;
}
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();
Arrays.sort(c1);
Arrays.sort(c2);
return new String(c1).equals(new String(c2));
}
}
isBrother(String s1, String s2) 形参有很大问题。s1、s2,一会儿能运行成功,一会儿不可以。换成 x、y就可以。


查看12道真题和解析