1032. Stream of Characters Hard

@problem@discussion
#Array#String#Design#Trie#Data Stream



1/**
2 * [1032] Stream of Characters
3 *
4 * 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.
5 * 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.
6 * Implement the StreamChecker class:
7 * 
8 * 	StreamChecker(String[] words) Initializes the object with the strings array words.
9 * 	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.
10 * 
11 *  
12 * Example 1:
13 * 
14 * Input
15 * ["StreamChecker", "query", "query", "query", "query", "query", "query", "query", "query", "query", "query", "query", "query"]
16 * [[["cd", "f", "kl"]], ["a"], ["b"], ["c"], ["d"], ["e"], ["f"], ["g"], ["h"], ["i"], ["j"], ["k"], ["l"]]
17 * Output
18 * [null, false, false, false, true, false, true, false, false, false, false, false, true]
19 * Explanation
20 * StreamChecker streamChecker = new StreamChecker(["cd", "f", "kl"]);
21 * streamChecker.query("a"); // return False
22 * streamChecker.query("b"); // return False
23 * streamChecker.query("c"); // return False
24 * streamChecker.query("d"); // return True, because 'cd' is in the wordlist
25 * streamChecker.query("e"); // return False
26 * streamChecker.query("f"); // return True, because 'f' is in the wordlist
27 * streamChecker.query("g"); // return False
28 * streamChecker.query("h"); // return False
29 * streamChecker.query("i"); // return False
30 * streamChecker.query("j"); // return False
31 * streamChecker.query("k"); // return False
32 * streamChecker.query("l"); // return True, because 'kl' is in the wordlist
33 * 
34 *  
35 * Constraints:
36 * 
37 * 	1 <= words.length <= 2000
38 * 	1 <= words[i].length <= 200
39 * 	words[i] consists of lowercase English letters.
40 * 	letter is a lowercase English letter.
41 * 	At most 4 * 10^4 calls will be made to query.
42 * 
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/stream-of-characters/
47// discuss: https://leetcode.com/problems/stream-of-characters/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51struct StreamChecker {
52        vec![]
53    }
54
55
56/** 
57 * `&self` means the method takes an immutable reference.
58 * If you need a mutable reference, change it to `&mut self` instead.
59 */
60impl StreamChecker {
61
62    fn new(words: Vec<String>) -> Self {
63        
64    }
65    
66    fn query(&self, letter: char) -> bool {
67        
68    }
69}
70
71/**
72 * Your StreamChecker object will be instantiated and called as such:
73 * let obj = StreamChecker::new(words);
74 * let ret_1: bool = obj.query(letter);
75 */
76
77// submission codes end
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82
83    #[test]
84    fn test_1032() {
85    }
86}
87


Back
© 2025 bowen.ge All Rights Reserved.