题解 | #查找兄弟单词#
查找兄弟单词
http://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
public class Main27 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String line = in.nextLine();
String[] strs = line.split(" ");
int val = Integer.valueOf(strs[0]);
String x = strs[strs.length - 2];
int k = Integer.valueOf(strs[strs.length - 1]);
ArrayList<String> list = new ArrayList<>();
for (int i = 1; i <= val; i++) {
if(isBrother(x,strs[i])){
list.add(strs[i]);
}
}
System.out.println(list.size());
if (list.size() >= k) {
Collections.sort(list);
System.out.println(list.get(k-1));
}
}
}
private static boolean isBrother(String x, String y) {
if (x.length() != y.length() || x.equals(y)) {
return false;
}
char[] charsX = x.toCharArray();
char[] charsY = y.toCharArray();
Arrays.sort(charsX);
Arrays.sort(charsY);
return new String(charsX).equals(new String(charsY));
}
凡岛公司福利 613人发布