|
1 | 1 | package com.fishercoder.solutions; |
2 | 2 |
|
3 | 3 | /** |
4 | | - * |
5 | 4 | * 208. Implement Trie (Prefix Tree) |
6 | 5 | * |
7 | 6 | * Implement a trie with insert, search, and startsWith methods. |
8 | 7 |
|
9 | 8 | Note: |
10 | | - You may assume that all inputs are consist of lowercase letters a-z.*/ |
| 9 | + You may assume that all inputs are consist of lowercase letters a-z. |
| 10 | + */ |
11 | 11 |
|
12 | 12 | public class _208 { |
13 | | - class TrieNode { |
| 13 | + public static class Solution1 { |
| 14 | + class TrieNode { |
14 | 15 |
|
15 | | - char val; |
16 | | - boolean isWord; |
17 | | - TrieNode[] children = new TrieNode[26]; |
| 16 | + char val; |
| 17 | + boolean isWord; |
| 18 | + TrieNode[] children = new TrieNode[26]; |
18 | 19 |
|
19 | | - // Initialize your data structure here. |
20 | | - public TrieNode() { |
21 | | - } |
| 20 | + // Initialize your data structure here. |
| 21 | + public TrieNode() { |
| 22 | + } |
22 | 23 |
|
23 | | - public TrieNode(char c) { |
24 | | - this.val = c; |
| 24 | + public TrieNode(char c) { |
| 25 | + this.val = c; |
| 26 | + } |
25 | 27 | } |
26 | | - } |
27 | 28 |
|
28 | | - public class Trie { |
29 | | - private TrieNode root; |
| 29 | + public class Trie { |
| 30 | + private TrieNode root; |
30 | 31 |
|
31 | | - public Trie() { |
32 | | - root = new TrieNode(); |
33 | | - root.val = ' ';//initialize root to be an empty char, this is a common practice as how Wiki defines Trie data structure as well |
34 | | - } |
| 32 | + public Trie() { |
| 33 | + root = new TrieNode(); |
| 34 | + root.val = ' ';//initialize root to be an empty char, this is a common practice as how Wiki defines Trie data structure as well |
| 35 | + } |
35 | 36 |
|
36 | | - // Inserts a word into the trie. |
37 | | - public void insert(String word) { |
38 | | - TrieNode node = root; |
39 | | - for (int i = 0; i < word.length(); i++) { |
40 | | - if (node.children[word.charAt(i) - 'a'] == null) { |
41 | | - node.children[word.charAt(i) - 'a'] = new TrieNode(word.charAt(i)); |
| 37 | + // Inserts a word into the trie. |
| 38 | + public void insert(String word) { |
| 39 | + TrieNode node = root; |
| 40 | + for (int i = 0; i < word.length(); i++) { |
| 41 | + if (node.children[word.charAt(i) - 'a'] == null) { |
| 42 | + node.children[word.charAt(i) - 'a'] = new TrieNode(word.charAt(i)); |
| 43 | + } |
| 44 | + node = node.children[word.charAt(i) - 'a']; |
42 | 45 | } |
43 | | - node = node.children[word.charAt(i) - 'a']; |
| 46 | + node.isWord = true; |
44 | 47 | } |
45 | | - node.isWord = true; |
46 | | - } |
47 | 48 |
|
48 | | - // Returns if the word is in the trie. |
49 | | - public boolean search(String word) { |
50 | | - TrieNode node = root; |
51 | | - for (int i = 0; i < word.length(); i++) { |
52 | | - if (node.children[word.charAt(i) - 'a'] == null) { |
53 | | - return false; |
| 49 | + // Returns if the word is in the trie. |
| 50 | + public boolean search(String word) { |
| 51 | + TrieNode node = root; |
| 52 | + for (int i = 0; i < word.length(); i++) { |
| 53 | + if (node.children[word.charAt(i) - 'a'] == null) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + node = node.children[word.charAt(i) - 'a']; |
54 | 57 | } |
55 | | - node = node.children[word.charAt(i) - 'a']; |
| 58 | + return node.isWord; |
56 | 59 | } |
57 | | - return node.isWord; |
58 | | - } |
59 | 60 |
|
60 | | - // Returns if there is any word in the trie |
61 | | - // that starts with the given prefix. |
62 | | - public boolean startsWith(String prefix) { |
63 | | - TrieNode node = root; |
64 | | - for (int i = 0; i < prefix.length(); i++) { |
65 | | - if (node.children[prefix.charAt(i) - 'a'] == null) { |
66 | | - return false; |
| 61 | + // Returns if there is any word in the trie |
| 62 | + // that starts with the given prefix. |
| 63 | + public boolean startsWith(String prefix) { |
| 64 | + TrieNode node = root; |
| 65 | + for (int i = 0; i < prefix.length(); i++) { |
| 66 | + if (node.children[prefix.charAt(i) - 'a'] == null) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + node = node.children[prefix.charAt(i) - 'a']; |
67 | 70 | } |
68 | | - node = node.children[prefix.charAt(i) - 'a']; |
| 71 | + return true; |
69 | 72 | } |
70 | | - return true; |
71 | 73 | } |
72 | | - } |
73 | 74 |
|
74 | | - // Your Trie object will be instantiated and called as such: |
75 | | - // Trie trie = new Trie(); |
76 | | - // trie.insert("somestring"); |
77 | | - // trie.search("key"); |
| 75 | + // Your Trie object will be instantiated and called as such: |
| 76 | + // Trie trie = new Trie(); |
| 77 | + // trie.insert("somestring"); |
| 78 | + // trie.search("key"); |
| 79 | + } |
78 | 80 | } |
0 commit comments