Skip to content

Commit 8e84e6c

Browse files
add 676
1 parent 002bd90 commit 8e84e6c

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|676|[Implement Magic Dictionary](https://leetcode.com/problems/implement-magic-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_676.java) | O(n^2) | O(n) | Medium |
26+
|674|[Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_674.java) | O(n^2) | O(1) | Easy |
2527
|672|[Bulb Switcher II](https://leetcode.com/problems/bulb-switcher-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_672.java) | O(1) | O(1) | Medium | Math
2628
|671|[Second Minimum Node In a Binary Tree](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_671.java) | O(n) | O(n) | Easy | Tree, DFS
2729
|670|[Maximum Swap](https://leetcode.com/problems/maximum-swap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_670.java) | O(n^2) | O(1) | Medium | String
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* 676. Implement Magic Dictionary
8+
* Implement a magic directory with buildDict, and search methods.
9+
* For the method buildDict, you'll be given a list of non-repetitive words to build a dictionary.
10+
* For the method search, you'll be given a word,
11+
* and judge whether if you modify exactly one character into another character in this word,
12+
* the modified word is in the dictionary you just built.
13+
14+
Example 1:
15+
16+
Input: buildDict(["hello", "leetcode"]), Output: Null
17+
Input: search("hello"), Output: False
18+
Input: search("hhllo"), Output: True
19+
Input: search("hell"), Output: False
20+
Input: search("leetcoded"), Output: False
21+
22+
Note:
23+
24+
You may assume that all the inputs are consist of lowercase letters a-z.
25+
For contest purpose, the test data is rather small by now.
26+
You could think about highly efficient algorithm after the contest.
27+
Please remember to RESET your class variables declared in class MagicDictionary,
28+
as static/class variables are persisted across multiple test cases. Please see here for more details.
29+
30+
*/
31+
public class _676 {
32+
33+
public static class Solution1 {
34+
public static class MagicDictionary {
35+
36+
Set<String> wordSet;
37+
38+
/**
39+
* Initialize your data structure here.
40+
*/
41+
public MagicDictionary() {
42+
wordSet = new HashSet<>();
43+
}
44+
45+
/**
46+
* Build a dictionary through a list of words
47+
*/
48+
public void buildDict(String[] dict) {
49+
for (String word : dict) {
50+
wordSet.add(word);
51+
}
52+
}
53+
54+
/**
55+
* Returns if there is any word in the trie that equals to the given word after modifying exactly one character
56+
*/
57+
public boolean search(String word) {
58+
for (String candidate : wordSet) {
59+
if (modifyOneChar(word, candidate)) {
60+
return true;
61+
}
62+
}
63+
return false;
64+
}
65+
66+
private boolean modifyOneChar(String word, String candidate) {
67+
if (word.length() != candidate.length()) {
68+
return false;
69+
}
70+
int diff = 0;
71+
for (int i = 0; i < word.length(); i++) {
72+
if (word.charAt(i) != candidate.charAt(i)) {
73+
diff++;
74+
}
75+
if (diff > 1) {
76+
return false;
77+
}
78+
}
79+
return diff == 1;
80+
}
81+
}
82+
}
83+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._676;
4+
import org.junit.Before;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _676Test {
11+
private static _676.Solution1.MagicDictionary magicDictionarySol1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
magicDictionarySol1 = new _676.Solution1.MagicDictionary();
16+
}
17+
18+
@Before
19+
public void cleanup() {
20+
magicDictionarySol1 = new _676.Solution1.MagicDictionary();
21+
}
22+
23+
@Test
24+
public void test1() {
25+
magicDictionarySol1.buildDict(new String[]{"hello", "leetcode"});
26+
assertEquals(false, magicDictionarySol1.search("hello"));
27+
assertEquals(true, magicDictionarySol1.search("hhllo"));
28+
assertEquals(false, magicDictionarySol1.search("hell"));
29+
assertEquals(false, magicDictionarySol1.search("leetcoded"));
30+
}
31+
}

0 commit comments

Comments
 (0)