Leetcode: 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 <= words.length <= 2000
1 <= words[i].length <= 2000
Words will only consist of lowercase English letters.
Queries will only consist of lowercase English letters.
The number of queries is at most 40000.
Store the words in the trie with reverse order, and check the query string from the end
class StreamChecker {
TrieNode root;
StringBuilder sb;
public StreamChecker(String[] words) {
this.root = new TrieNode();
this.sb = new StringBuilder();
for (String word : words) {
TrieNode node = root;
for (int i = word.length() - 1; i >= 0; i --) {
if (node.children[word.charAt(i) - 'a'] == null) {
node.children[word.charAt(i) - 'a'] = new TrieNode();
}
node = node.children[word.charAt(i) - '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.children[sb.charAt(i) - 'a'];
if (node != null && node.isWord) return true;
}
return false;
}
class TrieNode {
TrieNode[] children;
boolean isWord;
public TrieNode() {
this.children = new TrieNode[26];
this.isWord = false;
}
}
}
/**
* Your StreamChecker object will be instantiated and called as such:
* StreamChecker obj = new StreamChecker(words);
* boolean param_1 = obj.query(letter);
*/
Leetcode: Stream of Characters的更多相关文章
- 【leetcode】1032. Stream of Characters
题目如下: Implement the StreamChecker class as follows: StreamChecker(words): Constructor, init the data ...
- [Swift]LeetCode1032. 字符流 | Stream of Characters
Implement the StreamChecker class as follows: StreamChecker(words): Constructor, init the data struc ...
- [LeetCode] Read N Characters Given Read4 II - Call multiple times 用Read4来读取N个字符之二 - 多次调用
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
- [LeetCode] Read N Characters Given Read4 用Read4来读取N个字符
The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actua ...
- LeetCode Read N Characters Given Read4 II - Call multiple times
原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/ 题目: The ...
- LeetCode Read N Characters Given Read4
原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4/ 题目: The API: int read4(char *bu ...
- #Leetcode# 451. Sort Characters By Frequency
https://leetcode.com/problems/sort-characters-by-frequency/ Given a string, sort it in decreasing or ...
- LeetCode 451. Sort Characters By Frequency (根据字符出现频率排序)
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
- [LeetCode] 451. Sort Characters By Frequency 根据字符出现频率排序
Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...
随机推荐
- k8s安装之flannel.yaml
收藏一下,以后直接从这里cp过来用的. flannel新版 --- apiVersion: extensions/v1beta1 kind: PodSecurityPolicy metadata: n ...
- markdown锚点
转:https://blog.csdn.net/u012260238/article/details/87815170 markdown 语法文档:https://www.w3cschool.cn/l ...
- 《少年先疯队》第八次团队作业:Alpha冲刺第四天
前言 第四天冲刺会议 时间:2019.6.17 地点:宿舍 4.1 今日完成任务情况以及遇到的问题. 4.1.1今日完成任务情况 姚玉婷:管理员功能模块中,收费管理功能的实现. ...
- C# 6.0 中的新增功能(.NET Framework 4.6 与 Visual Studio 2015 )
C#6.0 在 2015 年7月随着.NET Framework 4.6 一同发布,后期发布了.NET Framework 4.6.1,4.6.2. 一.自动属性初始化(Auto-property i ...
- commons-dbutils 字段名称转换,支持驼峰字段名
你能遇到的问题,只要是普遍存在的,大家都会遇到,那么,就一定有现成的解决方案. 在阅读 commons-dbutils 的文档时, BeanHandler 的第二个参数可以达到这个目的.只需传入一个实 ...
- learning java AWT Pannel
import java.awt.*; public class PanelTest { public static void main(String[] args) { var f = new Fra ...
- strutsCRUD
Bookdao public class BookDao extends JsonBaseDao{ //分页查询书本信息 //根据书本id查询当个书本信息 public List<Map< ...
- shell脚本之字符串运算的使用
字符串运算符 下表列出了常用的字符串运算符,假定变量 a 为 "abc",变量 b 为 "efg": 运算符 说明 举例 = 检测两个字符串是否相等,相等返回 ...
- shell脚本编程基础之文件测试
文件测试判断条件 -e FILE:测试文件是否存在 -f FILE:测试文件是否为普通文件 -d FILE:测试指定路径是否为目录 -r FILE:测试当前用户对指定文件是否有读权限 -w FILE: ...
- 建立自己的键盘栈(shortcutkeyStack)
建立自己的键盘栈(shortcutkeyStack) 作为一名开发者, 快捷键是必不可少的, 并且各种开发工具都有提供快捷键. 但是各种工具(IDE,编辑器)因为历史或者其他不可抗原因(比如键盘的布局 ...