Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
86 lines (78 loc) · 2.75 KB

File metadata and controls

86 lines (78 loc) · 2.75 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.fishercoder.solutions;
public class _211 {
public static class Solution1 {
public static class WordDictionary {
WordNode root;
/**
* Initialize your data structure here.
*/
public WordDictionary() {
root = new WordNode();
}
public void addWord(String word) {
char[] chars = word.toCharArray();
addWord(chars, 0, root);
}
private void addWord(char[] chars, int index, WordNode parent) {
char c = chars[index];
int idx = c - 'a';
WordNode node = parent.children[idx];
if (node == null) {
node = new WordNode();
parent.children[idx] = node;
}
if (chars.length == index + 1) {
node.isLeaf = true;
return;
}
addWord(chars, ++index, node);
}
public boolean search(String word) {
return search(word.toCharArray(), 0, root);
}
/**
* This is also a beautifully designed recursive function.
*/
private boolean search(char[] chars, int index, WordNode parent) {
if (index == chars.length) {
if (parent.isLeaf) {
return true;
}
return false;
}
WordNode[] childNodes = parent.children;
char c = chars[index];
if (c == '.') {
for (int i = 0; i < childNodes.length; i++) {
WordNode n = childNodes[i];
if (n != null) {
boolean b = search(chars, index + 1, n);
if (b) {
return true;
}
}
}
return false;
}
WordNode node = childNodes[c - 'a'];
if (node == null) {
return false;
}
return search(chars, ++index, node);
}
/**
* This is a cool/standard design for a Trie node class.
*/
private class WordNode {
boolean isLeaf;
WordNode[] children = new WordNode[26];
}
}
/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.