Skip to content

Commit a8700cb

Browse files
varunu28fishercoder1534
authored andcommitted
Added _966.java (fishercoder1534#30)
Added _966.java
1 parent 9ad3f9c commit a8700cb

File tree

4 files changed

+136
-1
lines changed

4 files changed

+136
-1
lines changed

Leetcode.iml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module external.linked.project.id="Leetcode" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$" />
6+
<orderEntry type="inheritedJdk" />
7+
<orderEntry type="sourceFolder" forTests="false" />
8+
</component>
9+
</module>

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ Your ideas/fixes/algorithms are more than welcome!
2929

3030
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
3131
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
32-
|965|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_965.java) | O(n) | O(h) | |Easy| DFS, recursion
32+
|966|[Vowel Spellchecker](https://leetcode.com/problems/vowel-spellchecker/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_966.java) | O(hlogn) | O(n) | |Medium| Hash Table, String
33+
|965|[Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_965.java) | O(n) | O(h) | |Easy| DFS, recursion|
3334
|961|[N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_961.java) | O(n) | O(1) | |Easy|
3435
|953|[Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_953.java) | O(1) | O(1) | |Easy|
3536
|944|[Delete Columns to Make Sorted](https://leetcode.com/problems/delete-columns-to-make-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_944.java) | O(n) | O(1) | |Easy|
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
import java.util.Map;
6+
import java.util.Set;
7+
8+
/**
9+
* Given a wordlist, we want to implement a spellchecker that converts a query word into a correct word.
10+
*
11+
* For a given query word, the spell checker handles two categories of spelling mistakes:
12+
*
13+
* Capitalization: If the query matches a word in the wordlist (case-insensitive), then the query word is returned with
14+
* the same case as the case in the wordlist.
15+
* Example: wordlist = ["yellow"], query = "YellOw": correct = "yellow"
16+
* Example: wordlist = ["Yellow"], query = "yellow": correct = "Yellow"
17+
* Example: wordlist = ["yellow"], query = "yellow": correct = "yellow"
18+
* Vowel Errors: If after replacing the vowels ('a', 'e', 'i', 'o', 'u') of the query word with any vowel individually,
19+
* it matches a word in the wordlist (case-insensitive), then the query word is returned with the same case as the
20+
* match in the wordlist.
21+
* Example: wordlist = ["YellOw"], query = "yollow": correct = "YellOw"
22+
* Example: wordlist = ["YellOw"], query = "yeellow": correct = "" (no match)
23+
* Example: wordlist = ["YellOw"], query = "yllw": correct = "" (no match)
24+
* In addition, the spell checker operates under the following precedence rules:
25+
*
26+
* When the query exactly matches a word in the wordlist (case-sensitive), you should return the same word back.
27+
* When the query matches a word up to capitlization, you should return the first such match in the wordlist.
28+
* When the query matches a word up to vowel errors, you should return the first such match in the wordlist.
29+
* If the query has no matches in the wordlist, you should return the empty string.
30+
* Given some queries, return a list of words answer, where answer[i] is the correct word for query = queries[i].
31+
*
32+
* */
33+
34+
public class _966 {
35+
36+
public static class Solution {
37+
public String[] spellchecker(String[] wordlist, String[] queries) {
38+
Map<String, String> caseMap = new HashMap<>();
39+
Set<String> set = new HashSet<>();
40+
41+
// Case Part Mapping
42+
for (String word : wordlist) {
43+
if (!caseMap.containsKey(word.toLowerCase())) {
44+
caseMap.put(word.toLowerCase(), word);
45+
}
46+
47+
set.add(word);
48+
}
49+
50+
// Vowel Part Mapping
51+
Map<String, String> vowelMap = new HashMap<>();
52+
for (String word : wordlist) {
53+
String genericVal = makeGenericVowel(word);
54+
if (!vowelMap.containsKey(genericVal)) {
55+
vowelMap.put(genericVal, word);
56+
}
57+
}
58+
59+
String[] ans = new String[queries.length];
60+
61+
for (int i = 0; i < queries.length; i++) {
62+
if (set.contains(queries[i])) {
63+
ans[i] = queries[i];
64+
}
65+
else if (caseMap.containsKey(queries[i].toLowerCase())) {
66+
ans[i] = caseMap.get(queries[i].toLowerCase());
67+
}
68+
else if (vowelMap.containsKey(makeGenericVowel(queries[i]))) {
69+
ans[i] = vowelMap.get(makeGenericVowel(queries[i]));
70+
}
71+
else {
72+
ans[i] = "";
73+
}
74+
}
75+
76+
return ans;
77+
}
78+
79+
private String makeGenericVowel(String s) {
80+
String vowel = "aeiou";
81+
char[] ch = s.toLowerCase().toCharArray();
82+
for (int i = 0; i < ch.length; i++) {
83+
if (vowel.indexOf(ch[i]) != -1) {
84+
ch[i] = '#';
85+
}
86+
}
87+
88+
return String.valueOf(ch);
89+
}
90+
}
91+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._966;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
/**
12+
* Created by varunu28 on 1/01/19.
13+
*/
14+
15+
16+
public class _966Test {
17+
private static _966.Solution test;
18+
19+
@BeforeClass
20+
public static void setup() {
21+
test = new _966.Solution();
22+
}
23+
24+
25+
@Test
26+
public void test1() {
27+
assertEquals(
28+
Arrays.toString(new String[]{"kite","KiTe","KiTe","Hare","hare","","","KiTe","","KiTe"}),
29+
Arrays.toString(test
30+
.spellchecker(
31+
new String[]{"KiTe","kite","hare","Hare"},
32+
new String[]{"kite","Kite","KiTe","Hare","HARE","Hear","hear","keti","keet","keto"})));
33+
}
34+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy