Skip to content

Commit 96aaa13

Browse files
implement trie
1 parent f44ceca commit 96aaa13

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

‎MEDIUM/src/medium/ImplementTrie.java

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package medium;
2+
3+
public class ImplementTrie {
4+
class TrieNode {
5+
6+
char val;
7+
boolean isWord;
8+
TrieNode[] children = new TrieNode[26];
9+
10+
// Initialize your data structure here.
11+
public TrieNode() {}
12+
13+
public TrieNode(char c) {
14+
this.val = c;
15+
}
16+
}
17+
18+
public class Trie {
19+
private TrieNode root;
20+
21+
public Trie() {
22+
root = new TrieNode();
23+
root.val = ' ';//initialize root to be an empty char, this is a common practice as how Wiki defines Trie data structure as well
24+
}
25+
26+
// Inserts a word into the trie.
27+
public void insert(String word) {
28+
TrieNode node = root;
29+
for(int i = 0; i < word.length(); i++){
30+
if(node.children[word.charAt(i) - 'a'] == null){
31+
node.children[word.charAt(i) - 'a'] = new TrieNode(word.charAt(i));
32+
}
33+
node = node.children[word.charAt(i) - 'a'];
34+
}
35+
node.isWord = true;
36+
}
37+
38+
// Returns if the word is in the trie.
39+
public boolean search(String word) {
40+
TrieNode node = root;
41+
for(int i = 0; i < word.length(); i++){
42+
if(node.children[word.charAt(i) - 'a'] == null) return false;
43+
node = node.children[word.charAt(i) - 'a'];
44+
}
45+
return node.isWord;
46+
}
47+
48+
// Returns if there is any word in the trie
49+
// that starts with the given prefix.
50+
public boolean startsWith(String prefix) {
51+
TrieNode node = root;
52+
for(int i = 0; i < prefix.length(); i++){
53+
if(node.children[prefix.charAt(i) - 'a'] == null) return false;
54+
node = node.children[prefix.charAt(i) - 'a'];
55+
}
56+
return true;
57+
}
58+
}
59+
60+
// Your Trie object will be instantiated and called as such:
61+
// Trie trie = new Trie();
62+
// trie.insert("somestring");
63+
// trie.search("key");
64+
}

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
|441|[Arranging Coins](https://leetcode.com/problems/arrange-coins/)|[Solution](../../blob/master/EASY/src/easy/ArrangingCoins.java)| O(n)|O(1) | Easy|
77
|436|[Find Right Interval](https://leetcode.com/problems/find-right-interval/)|[Solution](../../blob/master/MEDIUM/src/medium/FindRightInterval.java) | O(nlogn) |O(n) | Medium| Binary Search
88
|435|[Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/)|[Solution](../../blob/master/MEDIUM/src/medium/NonOverlappingIntervals.java) | O(nlogn) |O(1) | Medium| Greedy
9+
|420|[Strong Password Checker](https://leetcode.com/problems/strong-password-checker/)|[Solution](../../blob/master/HARD/src/hard/StrongPasswordChecker.java)| ? | ? | Hard|
910
|419|[Battleships in a Board](https://leetcode.com/problems/battleships-in-a-board/)|[Solution](../../blob/master/MEDIUM/src/medium/BattleshipsinaBoard.java) | O(n^2) |O(1) | Medium| DFS
1011
|417|[Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/)|[Solution](../../blob/master/MEDIUM/src/medium/PacificAtlanticWaterFlow.java) | O(m*n*Max(m,n)) |O(m*n) | Medium| DFS
1112
|415|[Add Strings](https://leetcode.com/problems/add-strings/)|[Solution](../../blob/master/EASY/src/easy/AddStrings.java)| O(n)|O(1) | Easy|
@@ -39,6 +40,7 @@
3940
|223|[Rectangle Area](https://leetcode.com/problems/rectangle-area/)|[Solution](../../blob/master/EASY/src/easy/RectangleArea.java)| O(1)|O(1) | Easy|
4041
|219|[Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/)|[Solution](../../blob/master/EASY/src/easy/ContainsDuplicateII.java)| O(n)|O(n) | Easy| HashMap
4142
|209|[Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/)|[Solution](../../blob/master/MEDIUM/src/medium/MinimumSizeSubarraySum.java)| O(n)|O(1) | Medium|
43+
|208|[Implement Trie](https://leetcode.com/problems/implement-trie-prefix-tree/)|[Solution](../../blob/master/MEDIUM/src/medium/ImplementTrie.java)| O(n)|O(1) | Medium|
4244
|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)|[Solution](../../blob/master/EASY/src/easy/ReverseLinkedList.java)| O(n)|O(1) | Easy
4345
|205|[Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/)|[Solution](../../blob/master/EASY/src/easy/IsomorphicStrings.java)| O(n)|O(1) | Easy
4446
|200|[Number of Islands](https://leetcode.com/problems/number-of-islands/)|[Union Find](../../blob/master/MEDIUM/src/medium/NumberOfIslandsUnionFind.java) [DFS](../../blob/master/MEDIUM/src/medium/NumberofIslandsDFS.java)| O(m*n)|O(m*n) | Medium| Union Find, DFS

0 commit comments

Comments
 (0)