-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path22. Vowel Spellchecker
More file actions
40 lines (36 loc) · 1.37 KB
/
22. Vowel Spellchecker
File metadata and controls
40 lines (36 loc) · 1.37 KB
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
class Solution {
public String[] spellchecker(String[] wordlist, String[] queries) {
Set<String> exact = new HashSet<>();
Map<String, String> caseInsensitive = new HashMap<>();
Map<String, String> consonant = new HashMap<>();
for(String word : wordlist) {
exact.add(word);
caseInsensitive.putIfAbsent(word.toLowerCase(), word);
consonant.putIfAbsent(deVowel(word.toLowerCase()), word);
}
String[] result = new String[queries.length];
int i = 0;
for(String query : queries) {
if(exact.contains(query))
result[i] = query;
else if(caseInsensitive.containsKey(query.toLowerCase()))
result[i] = caseInsensitive.get(query.toLowerCase());
else if(consonant.containsKey(deVowel(query.toLowerCase())))
result[i] = consonant.get(deVowel(query.toLowerCase()));
else
result[i] ="";
i++;
}
return result;
}
public String deVowel(String word) {
StringBuilder sb = new StringBuilder();
for(char c : word.toCharArray()) {
sb.append(isVowel(c) ? "*" : c);
}
return sb.toString();
}
public boolean isVowel(char c) {
return (c == 'a' || c=='e' || c=='i' || c=='o' || c=='u');
}
}