Skip to content

Commit 4f9cc87

Browse files
committed
update
1 parent bed51a8 commit 4f9cc87

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

march_2021/week_4/spellCheck.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Solution {
2+
Set<String> perfect_word;
3+
Map<String, String> word_caseSensitive;
4+
Map<String, String> word_vowel;
5+
public String[] spellchecker(String[] wordlist, String[] queries) {
6+
perfect_word = new HashSet<String>();
7+
word_caseSensitive = new HashMap<String, String>();
8+
word_vowel = new HashMap<String, String>();
9+
10+
for (String word: wordlist) {
11+
perfect_word.add(word);
12+
13+
String wordLow = word.toLowerCase();
14+
word_caseSensitive.putIfAbsent(wordLow, word);
15+
16+
String wordLowVowel = devowel(wordLow);
17+
word_vowel.putIfAbsent(wordLowVowel, word);
18+
}
19+
20+
String[] ans = new String[queries.length];
21+
int t = 0;
22+
for (String s: queries)
23+
ans[t++] = solve(s);
24+
return ans;
25+
}
26+
27+
public String solve(String query) {
28+
if (perfect_word.contains(query))
29+
return query;
30+
31+
String queryLower = query.toLowerCase();
32+
if (word_caseSensitive.containsKey(queryLower))
33+
return word_caseSensitive.get(queryLower);
34+
35+
String queryLowerVowel = devowel(queryLower);
36+
if (word_vowel.containsKey(queryLowerVowel))
37+
return word_vowel.get(queryLowerVowel);
38+
39+
return "";
40+
}
41+
42+
public String devowel(String vowel) {
43+
StringBuilder sb = new StringBuilder();
44+
for (char c: vowel.toCharArray())
45+
sb.append(isVowel(c) ? '*' : c);
46+
return sb.toString();
47+
}
48+
49+
public boolean isVowel(char c) {
50+
return (c == 'a' || c == 'u' || c == 'o' || c == 'i' || c == 'e');
51+
}
52+
}

0 commit comments

Comments
 (0)