小王设计了一个简单的猜字谜游戏,游戏的谜面是一个错误的单词,比如nesw
,玩家需要猜出谜底库中正确的单词。
猜中的要求如下:
对于某个谜面和谜底单词,满足下面任一条件都表示猜中:
w
和e
的顺序,nwes
跟news
是可以完全对应的;woood
和wood
是一样的,它们去重后都是wod
请你写一个程序帮忙在谜底库中找到正确的谜底。
谜面是多个单词,都需要找到对应的谜底,如果找不到的话,返回not found
,
分隔,
分隔匹配到的正确单词列表,以,
分隔
如果找不到,返回not found
conection
connection,today
connection
bdni,wooood
bind,wrong,wood
bind,wood
将需要检查的字符串和目标字符串去重并排序,比较是否相等,全部都不相等则输出not found
import java.util.*;
/**
* Created with IntelliJ IDEA.
*
* @Author: Amos
* @E-mail: amos@amoscloud.com
* @Date: 2022/12/31
* @Time: 9:26
* @Description:
*/
public class Main0179 {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
String aim = scanner.nextLine();
String check = scanner.nextLine();
solution(aim, check);
}
}
private static void solution(String aim, String check) {
String[] split = check.split(",");
String[] seq1 = convert(aim.split(","));
String[] seq2 = convert(check.split(","));
List<String> res = new ArrayList<>();
for (String s1 : seq1) {
for (int i = 0; i < seq2.length; i++) {
if (s1.equals(seq2[i]) && !res.contains(split[i])) {
res.add(split[i]);
}
}
}
if (res.size() == 0) {
System.out.println("not found");
} else {
for (int i = 0; i < res.size(); i++) {
System.out.print(res.get(i));
if (i != res.size() - 1) {
System.out.print(",");
}
}
}
}
private static String[] convert(String[] strs) {
for (int i = 0; i < strs.length; i++) {
TreeSet<Character> characters = new TreeSet<>();
String str = strs[i];
for (char c : str.toLowerCase().toCharArray()) {
characters.add(c);
}
Character[] array = characters.toArray(new Character[0]);
char[] chars = new char[array.length];
for (int j = 0; j < array.length; j++) {
chars[j] = array[j];
}
strs[i] = new String(chars);
}
return strs;
}
}