|
| 1 | +/*: |
| 2 | +# 1032. Stream of Characters [Hard] |
| 3 | + https://leetcode.com/problems/stream-of-characters/ |
| 4 | + |
| 5 | + --- |
| 6 | + |
| 7 | +### Problem Statement: |
| 8 | + |
| 9 | + Implement the `StreamChecker` class as follows: |
| 10 | + |
| 11 | + + `StreamChecker(words)`: Constructor, init the data structure with the given words. |
| 12 | + + `query(letter)`: returns true if and only if for some `k >= 1`, the last `k` characters queried (in order from oldest to newest, including this letter just queried) spell one of the words in the given list. |
| 13 | + |
| 14 | +### Example: |
| 15 | + |
| 16 | + ``` |
| 17 | + StreamChecker streamChecker = new StreamChecker(["cd","f","kl"]); // init the dictionary. |
| 18 | + streamChecker.query('a'); // return false |
| 19 | + streamChecker.query('b'); // return false |
| 20 | + streamChecker.query('c'); // return false |
| 21 | + streamChecker.query('d'); // return true, because 'cd' is in the wordlist |
| 22 | + streamChecker.query('e'); // return false |
| 23 | + streamChecker.query('f'); // return true, because 'f' is in the wordlist |
| 24 | + streamChecker.query('g'); // return false |
| 25 | + streamChecker.query('h'); // return false |
| 26 | + streamChecker.query('i'); // return false |
| 27 | + streamChecker.query('j'); // return false |
| 28 | + streamChecker.query('k'); // return false |
| 29 | + streamChecker.query('l'); // return true, because 'kl' is in the wordlist |
| 30 | + |
| 31 | + ``` |
| 32 | + |
| 33 | + ### Notes: |
| 34 | + + 1 <= words.length <= 2000 |
| 35 | + + 1 <= words[i].length <= 2000 |
| 36 | + + Words will only consist of lowercase English letters |
| 37 | + + Queries will only consist of lowercase English letters. |
| 38 | + + The number of queries is at most 40000. |
| 39 | + |
| 40 | + ### Hint: |
| 41 | + + Put the words into a trie, and manage a set of pointers within that trie. |
| 42 | + |
| 43 | + */ |
| 44 | + |
| 45 | +// 17 / 17 test cases passed. |
| 46 | +// Status: Accepted |
| 47 | +// Runtime: 1908 ms |
| 48 | +// Memory Usage: 31.9 MB |
| 49 | + |
| 50 | +class Trie { |
| 51 | + var endOfWord: Bool = false |
| 52 | + var chars = [Character: Trie]() |
| 53 | +} |
| 54 | + |
| 55 | +class StreamChecker { |
| 56 | + var queryStream = [Character]() |
| 57 | +// var queryStream = "" |
| 58 | + var root: Trie |
| 59 | + |
| 60 | + init(_ words: [String]) { |
| 61 | + root = Trie() |
| 62 | + |
| 63 | + for word in words { |
| 64 | + var node = root |
| 65 | + for char in word.reversed() { |
| 66 | + if node.chars[char] == nil { |
| 67 | + node.chars[char] = Trie() |
| 68 | + } |
| 69 | + node = node.chars[char]! |
| 70 | + } |
| 71 | + node.endOfWord = true |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + func query(_ letter: Character) -> Bool { |
| 76 | + queryStream.append(letter) |
| 77 | +// queryStream += String(letter) |
| 78 | + |
| 79 | + var node = root |
| 80 | + for char in queryStream.reversed() { |
| 81 | + guard let next = node.chars[char] else { |
| 82 | + return false |
| 83 | + } |
| 84 | + |
| 85 | + if next.endOfWord { |
| 86 | + return true |
| 87 | + } |
| 88 | + |
| 89 | + node = next |
| 90 | + } |
| 91 | + |
| 92 | + return false |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | + // Your StreamChecker object will be instantiated and called as such: |
| 97 | + |
| 98 | + let obj = StreamChecker(["cd","f","kl"]) |
| 99 | + obj.query("a") |
| 100 | + obj.query("b") |
| 101 | + obj.query("c") |
| 102 | + obj.query("d") |
| 103 | + obj.query("e") |
| 104 | + obj.query("f") |
| 105 | + obj.query("g") |
| 106 | + obj.query("h") |
| 107 | + obj.query("i") |
| 108 | + obj.query("j") |
| 109 | + obj.query("k") |
| 110 | + obj.query("l") |
| 111 | + |
0 commit comments