Skip to content

Commit a1da55f

Browse files
refactor 208
1 parent 846eaf5 commit a1da55f

File tree

1 file changed

+52
-50
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+52
-50
lines changed
Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,80 @@
11
package com.fishercoder.solutions;
22

33
/**
4-
*
54
* 208. Implement Trie (Prefix Tree)
65
*
76
* Implement a trie with insert, search, and startsWith methods.
87
98
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+
*/
1111

1212
public class _208 {
13-
class TrieNode {
13+
public static class Solution1 {
14+
class TrieNode {
1415

15-
char val;
16-
boolean isWord;
17-
TrieNode[] children = new TrieNode[26];
16+
char val;
17+
boolean isWord;
18+
TrieNode[] children = new TrieNode[26];
1819

19-
// Initialize your data structure here.
20-
public TrieNode() {
21-
}
20+
// Initialize your data structure here.
21+
public TrieNode() {
22+
}
2223

23-
public TrieNode(char c) {
24-
this.val = c;
24+
public TrieNode(char c) {
25+
this.val = c;
26+
}
2527
}
26-
}
2728

28-
public class Trie {
29-
private TrieNode root;
29+
public class Trie {
30+
private TrieNode root;
3031

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+
}
3536

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'];
4245
}
43-
node = node.children[word.charAt(i) - 'a'];
46+
node.isWord = true;
4447
}
45-
node.isWord = true;
46-
}
4748

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'];
5457
}
55-
node = node.children[word.charAt(i) - 'a'];
58+
return node.isWord;
5659
}
57-
return node.isWord;
58-
}
5960

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'];
6770
}
68-
node = node.children[prefix.charAt(i) - 'a'];
71+
return true;
6972
}
70-
return true;
7173
}
72-
}
7374

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+
}
7880
}

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