1032. Stream of Characters
package LeetCode_1032 /** * 1032. Stream of Characters * https://leetcode.com/problems/stream-of-characters/ * Implement the StreamChecker class as follows: StreamChecker(words): Constructor, init the data structure with the given words. 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. Example: StreamChecker streamChecker = new StreamChecker(["cd","f","kl"]); // init the dictionary. 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 Note: 1. 1 <= words.length <= 2000 2. 1 <= words[i].length <= 2000 3. Words will only consist of lowercase English letters. 4. Queries will only consist of lowercase English letters. 5. The number of queries is at most 40000. * */
/* * Solution: Trie Tree, * Time complexity: O(M) M is the longest length of the word, * Space complexity: O(M*N), N is the number of word * */ class StreamChecker(words: Array<String>) { var root: Trie? = null //save letter to help to check val sb = StringBuilder() init { root = Trie() for (word in words) { addWord(word) } } fun query(letter: Char): Boolean { sb.append(letter) var node = root //here also check form last for (i in sb.length - 1 downTo 0) { if (node == null) { continue } val c = sb[i] node = node.children[c - 'a'] if (node != null && node.end) { return true } } return false } private fun addWord(word: String) { var node = root //create by reverse order, because can check from last letter //for (ch in word) { for (i in word.length - 1 downTo 0) { val pos = word[i] - 'a' if (node!!.children[pos] == null) { node.children[pos] = Trie() } //set the children for current node node = node.children[pos] } node!!.end = true } class Trie { var end = false var children = arrayOfNulls<Trie>(26) } } /** * Your StreamChecker object will be instantiated and called as such: * var obj = StreamChecker(words) * var param_1 = obj.query(letter) */
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)