forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpellChecker.java
61 lines (44 loc) · 1.65 KB
/
SpellChecker.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
60
61
class Solution {
// TC : O(no of queries + no of el in wordList *2)
// SC : O(n)
public String[] spellchecker(String[] wordlist, String[] queries) {
HashSet<String> wordSet = new HashSet<>();
for(String word: wordlist){
wordSet.add(word);
}
Map<String, String> map = new HashMap<>();
for(String word : wordlist){
String lowerCaseStr = word.toLowerCase();
map.putIfAbsent(lowerCaseStr, word);
String vowelRemovedStr = getVowelRemovedStr(word);
System.out.println(vowelRemovedStr + " " + word);
map.putIfAbsent(vowelRemovedStr, word);
}
for(int i=0;i<queries.length;i++){
if(wordSet.contains(queries[i])){
continue;
}
String lowerCaseQuery = queries[i].toLowerCase();
if(map.containsKey(lowerCaseQuery)){
queries[i] = map.get(lowerCaseQuery);
continue;
}
String vowelRemovedQuery = getVowelRemovedStr(queries[i]);
if(map.containsKey(vowelRemovedQuery)){
queries[i] = map.get(vowelRemovedQuery);
continue;
}
queries[i] = "";
}
return queries;
}
private String getVowelRemovedStr(String word){
char[] wordChar = word.toLowerCase().toCharArray();
for(int i=0;i<word.length();i++){
if(wordChar[i] == 'a' || wordChar[i] == 'e'|| wordChar[i] == 'i'|| wordChar[i] == 'o'||wordChar[i] == 'u'){
wordChar[i] = '*';
}
}
return new String(wordChar);
}
}