Skip to content

Commit afd0b44

Browse files
author
mohit
committed
Added corner case while querying a string in trie
1 parent d872c41 commit afd0b44

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/main/java/main/java/videos/Tries.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public static void main(String[] args) {
1414
final Trie trie = new Trie();
1515
setOfStrings.forEach(trie::insert);
1616
System.out.println(trie.query("psst"));
17+
System.out.println(trie.query("psstq"));
1718
trie.update("qqrs", "psst");
1819
System.out.println(trie.query("qqrs"));
1920
System.out.println(trie.query("psst"));
@@ -30,10 +31,11 @@ public Trie() {
3031
public int query(final String s) {
3132
TrieNode current = root;
3233
for (int i = 0; i < s.length(); i++) {
33-
if (current == null) {
34-
return 0;
35-
}
36-
current = current.next(s.charAt(i));
34+
if(current==null || current.next(s.charAt(i))==null) {
35+
return 0;
36+
}
37+
else
38+
current=current.next(s.charAt(i));
3739
}
3840
return current.terminating;
3941
}

0 commit comments

Comments
 (0)