题解 | #查找兄弟单词#
查找兄弟单词
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.hasNextLine()) {
String[] str = sc.nextLine().split("\\s+");
int count = Integer.parseInt(str[0]);
String test = str[str.length-2];
int k = Integer.parseInt(str[str.length-1]);
String[] original = new String[count];
for(int i = 0; i < count; i++) {
original[i] = str[i+1];
}
char[] chs = test.toCharArray();
sort(chs);
// Use this to sort this string, in case to judge if this element is equals another string
String sss = new String(chs);
List<String> s = new ArrayList<>();
for(int i = 0; i < original.length; i++) {
if(test.length() == original[i].length() && !test.equals(original[i])) {
char[] cs = original[i].toCharArray();
// Sort this array, compare to this check string
sort(cs);
if((new String(cs)).equals(sss)) {
s.add(original[i]);
}
}
}
System.out.println(s.size());
if(k <= s.size() -1 ) {
Collections.sort(s);
System.out.println(s.get(k-1));
}
}
}
// Sort method, a little skill, Insertion sort function
private static void sort(char[] strs) {
for(int i = 0; i < strs.length; i++) {
for(int j = i; j > 0; j--) {
if(strs[j] < strs[j-1]) {
char tmp = strs[j-1];
strs[j-1] = strs[j];
strs[j] = tmp;
}
}
}
}
}
// Thank you for your appreciation