All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g1001_1100.s1032_stream_of_characters.StreamChecker Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g1001_1100.s1032_stream_of_characters;

// #Hard #Array #String #Design #Trie #Data_Stream
// #2022_02_27_Time_137_ms_(81.54%)_Space_148.2_MB_(53.37%)

/**
 * 1032 - Stream of Characters\.
 *
 * Hard
 *
 * Design an algorithm that accepts a stream of characters and checks if a suffix of these characters is a string of a given array of strings `words`.
 *
 * For example, if `words = ["abc", "xyz"]` and the stream added the four characters (one by one) `'a'`, `'x'`, `'y'`, and `'z'`, your algorithm should detect that the suffix `"xyz"` of the characters `"axyz"` matches `"xyz"` from `words`.
 *
 * Implement the `StreamChecker` class:
 *
 * *   `StreamChecker(String[] words)` Initializes the object with the strings array `words`.
 * *   `boolean query(char letter)` Accepts a new character from the stream and returns `true` if any non-empty suffix from the stream forms a word that is in `words`.
 *
 * **Example 1:**
 *
 * **Input** ["StreamChecker", "query", "query", "query", "query", "query", "query", "query", "query", "query", "query", "query", "query"] [[["cd", "f", "kl"]], ["a"], ["b"], ["c"], ["d"], ["e"], ["f"], ["g"], ["h"], ["i"], ["j"], ["k"], ["l"]]
 *
 * **Output:** [null, false, false, false, true, false, true, false, false, false, false, false, true]
 *
 * **Explanation:** 
 *
 *     StreamChecker streamChecker = new StreamChecker(["cd", "f", "kl"]); 
 *     streamChecker.query("a"); // return False 
 *     streamChecker.query("b"); // return False 
 *     streamChecker.query("c"); // return False 
 *     streamChecker.query("d"); // return True, because 'cd' is in the wordlist 
 *     streamChecker.query("e"); // return False 
 *     streamChecker.query("f"); // return True, because 'f' is in the wordlist 
 *     streamChecker.query("g"); // return False 
 *     streamChecker.query("h"); // return False 
 *     streamChecker.query("i"); // return False 
 *     streamChecker.query("j"); // return False 
 *     streamChecker.query("k"); // return False 
 *     streamChecker.query("l"); // return True, because 'kl' is in the wordlist
 *
 * **Constraints:**
 *
 * *   `1 <= words.length <= 2000`
 * *   `1 <= words[i].length <= 2000`
 * *   `words[i]` consists of lowercase English letters.
 * *   `letter` is a lowercase English letter.
 * *   At most 4 * 104 calls will be made to query.
**/
public class StreamChecker {
    static class Node {
        Node[] child;
        boolean isEnd;

        Node() {
            child = new Node[26];
        }
    }

    private final StringBuilder sb;
    private final Node root;

    public void insert(String s) {
        Node curr = root;
        for (int i = s.length() - 1; i >= 0; i--) {
            char c = s.charAt(i);
            if (curr.child[c - 'a'] == null) {
                curr.child[c - 'a'] = new Node();
            }
            curr = curr.child[c - 'a'];
        }
        curr.isEnd = true;
    }

    public StreamChecker(String[] words) {
        root = new Node();
        sb = new StringBuilder();
        for (String s : words) {
            insert(s);
        }
    }

    public boolean query(char letter) {
        sb.append(letter);
        Node curr = root;
        for (int i = sb.length() - 1; i >= 0; i--) {
            char c = sb.charAt(i);
            if (curr.child[c - 'a'] == null) {
                return false;
            }
            if (curr.child[c - 'a'].isEnd) {
                return true;
            }
            curr = curr.child[c - 'a'];
        }
        return false;
    }
}

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy