Skip to content

Commit

Permalink
Updated 2 solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
RodneyShag committed Jul 25, 2019
1 parent f2b9e39 commit df63989
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,10 @@ public TrieNode getChild(char ch) {

class Trie {
TrieNode root = new TrieNode();

Trie(){} // default constructor

Trie(String[] words) {
for (String word : words) {
add(word);
}
}

public void add(String str) {
TrieNode curr = root;
for (int i = 0; i < str.length(); i++) {
Character ch = str.charAt(i);
for (char ch : str.toCharArray()) {
curr.putChildIfAbsent(ch);
curr = curr.getChild(ch);
curr.size++;
Expand All @@ -59,10 +50,7 @@ public void add(String str) {

public int find(String prefix) {
TrieNode curr = root;

/* Traverse down tree to end of our prefix */
for (int i = 0; i < prefix.length(); i++) {
Character ch = prefix.charAt(i);
for (char ch : prefix.toCharArray()) {
curr = curr.getChild(ch);
if (curr == null) {
return 0;
Expand Down
16 changes: 2 additions & 14 deletions Data Structures/Trie/Contacts/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,10 @@ public TrieNode getChild(char ch) {

class Trie {
TrieNode root = new TrieNode();

Trie(){} // default constructor

Trie(String[] words) {
for (String word : words) {
add(word);
}
}

public void add(String str) {
TrieNode curr = root;
for (int i = 0; i < str.length(); i++) {
Character ch = str.charAt(i);
for (char ch : str.toCharArray()) {
curr.putChildIfAbsent(ch);
curr = curr.getChild(ch);
curr.size++;
Expand All @@ -59,10 +50,7 @@ public void add(String str) {

public int find(String prefix) {
TrieNode curr = root;

/* Traverse down tree to end of our prefix */
for (int i = 0; i < prefix.length(); i++) {
Character ch = prefix.charAt(i);
for (char ch : prefix.toCharArray()) {
curr = curr.getChild(ch);
if (curr == null) {
return 0;
Expand Down

0 comments on commit df63989

Please sign in to comment.