题解 | #查找兄弟单词#
查找兄弟单词
http://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
import java.util.*;
public class Main {
public static boolean checkBrother(String str1,String str2){
int []arr = new int[26];
char []ch1 = str1.toCharArray();
char []ch2 = str2.toCharArray();
for(int i = 0; i<ch1.length;i++){
arr[ch1[i]-'a']++;
arr[ch2[i]-'a']--;
}
for(int i =0;i<26;i++){
if(arr[i]!=0){
return false;
}
}
return true;
}
public static void main(String []args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int N = sc.nextInt();
String []str = new String[N];
for(int i=0;i<N;i++){
str[i] = sc.next();
}
String findstr =sc.next();
int ind = sc.nextInt();
ArrayList<String>list = new ArrayList<>();
for(int i = 0;i<N;i++){
if((str[i].length() == findstr.length())&& (!str[i].equals(findstr))){
if(checkBrother(findstr,str[i])){
list.add(str[i]);
}
}
}
System.out.println(list.size());
Collections.sort(list);
if(list.size()>=ind){
System.out.println(list.get(ind - 1));
}
}
}
}
public class Main {
public static boolean checkBrother(String str1,String str2){
int []arr = new int[26];
char []ch1 = str1.toCharArray();
char []ch2 = str2.toCharArray();
for(int i = 0; i<ch1.length;i++){
arr[ch1[i]-'a']++;
arr[ch2[i]-'a']--;
}
for(int i =0;i<26;i++){
if(arr[i]!=0){
return false;
}
}
return true;
}
public static void main(String []args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int N = sc.nextInt();
String []str = new String[N];
for(int i=0;i<N;i++){
str[i] = sc.next();
}
String findstr =sc.next();
int ind = sc.nextInt();
ArrayList<String>list = new ArrayList<>();
for(int i = 0;i<N;i++){
if((str[i].length() == findstr.length())&& (!str[i].equals(findstr))){
if(checkBrother(findstr,str[i])){
list.add(str[i]);
}
}
}
System.out.println(list.size());
Collections.sort(list);
if(list.size()>=ind){
System.out.println(list.get(ind - 1));
}
}
}
}