-
Notifications
You must be signed in to change notification settings - Fork 45
/
_966.java
42 lines (36 loc) · 1.21 KB
/
_966.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;
/**
* LeetCode 966 - Vowel Spellchecker
*
* Mapper
*/
public class _966 {
class Checker {
Map<String, String> data = new HashMap<>();
Function<String, String> mapper;
Checker(String[] words, Function<String, String> mapper) {
this.mapper = mapper;
for (String word : words) {
data.putIfAbsent(mapper.apply(word), word);
}
}
String query(String query) {
return data.get(mapper.apply(query));
}
}
public String[] spellchecker(String[] wordlist, String[] queries) {
Checker c1 = new Checker(wordlist, s -> s);
Checker c2 = new Checker(wordlist, s -> s.toLowerCase());
Checker c3 = new Checker(wordlist, s -> s.toLowerCase().replaceAll("[aeiou]", "?"));
String[] res = new String[queries.length];
for (int i = 0; i < res.length; i++) {
String q = queries[i];
res[i] = Stream.of(c1.query(q), c2.query(q), c3.query(q)).filter(s -> s != null).findFirst().orElse("");
}
return res;
}
}