题解 | #查找兄弟单词#
查找兄弟单词
http://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
String inputSt = in.nextLine();
String[] st = inputSt.split(" ");
int num = Integer.valueOf(st[0]);
String s1 = st[st.length - 2];
String s = test1(s1);
int index = Integer.valueOf(st[st.length - 1]);
List<string> stList = new ArrayList<string>();
for (int i=1;i<=num;i++) {
if (test1(st[i]).equals(s) && !st[i].equals(s1)) {
stList.add(st[i]);
}
}
Collections.sort(stList);
System.out.println(stList.size());
if (stList.size() >= index) {
System.out.println(stList.get(index-1));
}
}
}
private static String test1(String str) {
char[] chars = str.toCharArray();
List<string> tr = new ArrayList<string>();
StringBuffer sb = new StringBuffer();
for (char ch : chars) {
tr.add(String.valueOf(ch));
}
Collections.sort(tr);
for (String s : tr) {
sb.append(s);
}
return sb.toString();
}
}</string></string></string></string>