Skip to content

Commit

Permalink
W4:Q3: StreamofCharacters
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishek Surve committed Aug 23, 2020
1 parent 0981c94 commit 2213b40
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Q. [The Maze](https://leetcode.com/explore/challenge/card/august-leetcoding-chal

Q. [Random Point in Non-overlapping Rectangles](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/552/week-4-august-22nd-august-28th/3433/) ➡️ [Solution](https://github.com/abhisheksurve45/leetcode-aug-2020/blob/master/WEEK3/RandomPointinNonoverlappingRectangles.java)

Q. [Stream of Characters](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/552/week-4-august-22nd-august-28th/3434/) ➡️ [Solution](https://github.com/abhisheksurve45/leetcode-aug-2020/blob/master/WEEK3/StreamofCharacters.java)

## WEEK 5 🚧

[Coming soon](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/)
Expand Down
53 changes: 53 additions & 0 deletions WEEK4/StreamofCharacters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
public class StreamofCharacters {

class StreamChecker {

private class TrieNode {
private TrieNode[] next = new TrieNode[26];
private boolean isWord;
}

private TrieNode root;
private StringBuilder sb;

public StreamChecker(String[] words) {

this.root = new TrieNode();
this.sb = new StringBuilder();

for (String str : words) {
int len = str.length();
TrieNode node = root;

for (int i = len - 1; i >= 0; i--) {
char c = str.charAt(i);
if (node.next[c - 'a'] == null) node.next[c - 'a'] = new TrieNode();
node = node.next[c - 'a'];
}

node.isWord = true;
}
}

public boolean query(char letter) {

sb.append(letter);
TrieNode node = root;

for (int i = sb.length() - 1; i >= 0 && node != null; i--) {
node = node.next[sb.charAt(i) - 'a'];
if (node != null && node.isWord) {
return true;
}
}

return false;
}
}

/**
* Your StreamChecker object will be instantiated and called as such:
* StreamChecker obj = new StreamChecker(words);
* boolean param_1 = obj.query(letter);
*/
}

0 comments on commit 2213b40

Please sign in to comment.