自己写的。主要时排序。
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
import java.util.*;
public class Main {
public static boolean isBroather(String str1, String str2) {
char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();
Arrays.sort(arr1);
Arrays.sort(arr2);
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String[] in = sc.nextLine().split(" ");
int n = in.length;
String x = in[n - 2];
int k = Integer.valueOf(in[n - 1]);
List<String> broathers = new ArrayList<>(); //存储兄弟单词
for (int i = 1; i < n - 2; i++) {
if (in[i].length() == x.length() && x.compareTo(in[i]) != 0 &&
isBroather(in[i], x)) {
broathers.add(in[i]);
}
}
System.out.println(broathers.size());
Collections.sort(broathers); //排序
if (k <= broathers.size()) {
int i = 1;
for (String s : broathers) {
if (i == k) {
System.out.println(s);
}
i++;
}
}
}
}
}
快手公司福利 1244人发布