题解 | #查找兄弟单词#
查找兄弟单词
https://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);
int count=in.nextInt();//
String[] strs=new String[count];
for(int i=0;i<count;i++)
{
strs[i]=in.next();
}
String x=in.next();
int index=in.nextInt();
ArrayList<String> browords=new ArrayList<>();
for(int i=0;i<strs.length;i++)
{
if((!strs[i].equals(x))&&strs[i].length()==x.length())
{
char[] chStr=x.toCharArray();//对比!
char[] word=strs[i].toCharArray();//
int temp=0;
for(int j=0;j<chStr.length;j++)
{
for(int j2=0;j2<word.length;j2++)
{
if(word[j]==chStr[j2])
{
chStr[j2]='0';
temp++;
break;
}
}
}
if(temp==chStr.length)
{
browords.add(strs[i]);
}
}
}
System.out.println(browords.size());
if(index>0&&index<browords.size())
{
Collections.sort(browords);
System.out.println(browords.get(index-1));
}
// 注意 hasNext 和 hasNextLine 的区别
}
}
//1、兄弟有哪些、以及按序输出第一个---》只,。
//需要sort输出第一个
//2、那个相同、不同------?只用比较所有的字母是否相同且长度相同
是否相同,一个字母一个字母的比较,两层for循环
查看14道真题和解析