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: ...
随机推荐
- 一个自己编写的简单AC自动机代码-----AC automata get √
最近一直在优化项目中字符串匹配的问题,于是就想起了自动机,之前也看过一些文章,一直没有实现,现在项目中要用,然后又看了一些关于AC自动机的文章,这里实现了一个简单的AC自动机的小接口,我是实现自动机状 ...
- Python for Email
SMTP 正如 HTTP 是计算机用来通过因特网发送网页的协议,简单邮件传输协议(SMTP) 是用于发送电子邮件的协议 import smtplib 发送电子邮件 连接到SMTP服务器 smtpObj ...
- Java精通并发-锁升级与偏向锁深入解析
对于synchronized关键字,我们在实际使用时可能经常听说用它是一个非常重的操作,其实这个“重”是要针对JDK的版本来说的,如今JDK已经到了12版本了,其实对这个关键字一直是存在偏见的,它底层 ...
- hdu6715 算术 2019百度之星初赛3-1003
题目地址 http://acm.hdu.edu.cn/showproblem.php?pid=6715 题解 还是不会这题的容斥做法qwq.hjw当场写了个容斥A了.我推了个莫反,但是没反应过来我的式 ...
- SpringBoot学习(四)开发web应用
Spring Boot非常适合web应用程序开发.可以使用嵌入式Tomcat.Jetty.Undertow或Netty创建自包含的HTTP服务器.大多数web应用程序使用spring-boot-sta ...
- 使用Optional优雅处理null
先假设一个场景.如下所示 public class Person { private String name; public Person() { } public Person(String nam ...
- [CSS] Create a Card Flip Animation with CSS
Animation can be a powerful way to enhance a user experience. In this lesson, we'll walk through the ...
- H3CNE学习4 命令行简介
一.命令 1.简单命令 2.给路由器接口配置IP地址 3.常用命令 4.删除配置
- python与各数据库的交互
from redis import StrictRedis from pymongo import MongoClient import pymysql #redis客户端 redis_cli = S ...
- [matlab工具箱] 神经网络Neural Net
//目的是学习在BP神经网络的基础上添加遗传算法,蚁群算法等优化算法来优化网络,这是后话. 先简单了解了MATLAB中的神经网络工具箱,工具箱功能还是非常强大的,已经可以拟合出非常多的曲线来分析了. ...