forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_966.java
59 lines (51 loc) · 1.92 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.fishercoder.solutions;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class _966 {
public static class Solution1 {
public String[] spellchecker(String[] wordlist, String[] queries) {
Map<String, String> caseMap = new HashMap<>();
Set<String> set = new HashSet<>();
// Case Part Mapping
for (String word : wordlist) {
if (!caseMap.containsKey(word.toLowerCase())) {
caseMap.put(word.toLowerCase(), word);
}
set.add(word);
}
// Vowel Part Mapping
Map<String, String> vowelMap = new HashMap<>();
for (String word : wordlist) {
String genericVal = makeGenericVowel(word);
if (!vowelMap.containsKey(genericVal)) {
vowelMap.put(genericVal, word);
}
}
String[] ans = new String[queries.length];
for (int i = 0; i < queries.length; i++) {
if (set.contains(queries[i])) {
ans[i] = queries[i];
} else if (caseMap.containsKey(queries[i].toLowerCase())) {
ans[i] = caseMap.get(queries[i].toLowerCase());
} else if (vowelMap.containsKey(makeGenericVowel(queries[i]))) {
ans[i] = vowelMap.get(makeGenericVowel(queries[i]));
} else {
ans[i] = "";
}
}
return ans;
}
private String makeGenericVowel(String s) {
String vowel = "aeiou";
char[] ch = s.toLowerCase().toCharArray();
for (int i = 0; i < ch.length; i++) {
if (vowel.indexOf(ch[i]) != -1) {
ch[i] = '#';
}
}
return String.valueOf(ch);
}
}
}