题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
import java.util.*; import java.util.regex.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case int n = in.nextInt(); String[] strs = new String[n]; for(int i=0;i<n;i++) strs[i]=in.next(); Arrays.sort(strs); String str = in.next(); int k = in.nextInt(); int count = 0; List<Character> list = new ArrayList<Character>(); List<String> resList = new ArrayList<String>(); for(int i=0;i<str.length();i++) list.add(str.charAt(i)); Collections.sort(list); Pattern p = Pattern.compile("[^"+str+"]"); for(String s : strs) { List<Character> list2 = new ArrayList<Character>(); for(int i=0;i<s.length();i++) list2.add(s.charAt(i)); Collections.sort(list2); boolean isEqual = list.equals(list2); if(!p.matcher(s).find() && !s.equals(str) && s.length()==str.length() && isEqual) { count++; resList.add(s); } } System.out.println(count); if(k<=resList.size()) System.out.println(resList.toArray()[k-1]); } } }