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的更多相关文章

  1. 【leetcode】1032. Stream of Characters

    题目如下: Implement the StreamChecker class as follows: StreamChecker(words): Constructor, init the data ...

  2. [Swift]LeetCode1032. 字符流 | Stream of Characters

    Implement the StreamChecker class as follows: StreamChecker(words): Constructor, init the data struc ...

  3. [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 ...

  4. [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 ...

  5. LeetCode Read N Characters Given Read4 II - Call multiple times

    原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/ 题目: The ...

  6. LeetCode Read N Characters Given Read4

    原题链接在这里:https://leetcode.com/problems/read-n-characters-given-read4/ 题目: The API: int read4(char *bu ...

  7. #Leetcode# 451. Sort Characters By Frequency

    https://leetcode.com/problems/sort-characters-by-frequency/ Given a string, sort it in decreasing or ...

  8. LeetCode 451. Sort Characters By Frequency (根据字符出现频率排序)

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

  9. [LeetCode] 451. Sort Characters By Frequency 根据字符出现频率排序

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

随机推荐

  1. python __main__ 里的变量是全局变量 因此在函数里面可以访问到

    __main__ and scoping in python from:https://stackoverflow.com/questions/4775579/main-and-scoping-in- ...

  2. LGOJ P2048 [NOI2010]超级钢琴

    题目描述 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i个音符的美妙度为Ai,其中A ...

  3. No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.lang.String, java.lang.String>]

    java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...

  4. Sharding-JDBC(二)2.0.3版本实践

    目录 一.Sharding-JDBC依赖 二.分片策略 1. 标准分片策略 2. 复合分片策略 3. Inline表达式分片策略 4. 通过Hint而非SQL解析的方式分片的策略 5. 不分片的策略 ...

  5. linux autofs自动挂载

    autofs:自动挂载器 自动挂载器是一个监视目录的守护进程,并在目标子目录被引用时,自动执行预定义的NFS挂载 自动挂载器由autofs服务脚本管理 自动挂载器由auto.master配置文件进行配 ...

  6. gdb, pdb笔记

    gdb gdb --args yourprogram 常用命令 r(run):从头开始运行 c(continue):继续运行 b(breakpoint) filepath:line or namesp ...

  7. 针对jar里面的图片不显示问题

    做了一个html生产pdf案例. 然后把图片放到resource/static/model/img下面,生成jar包运行,发现图片不显示, 发现html里面的src必须是http域名开头的图片. 下面 ...

  8. centos7.3 安装 mysql-5.7.13

    系统环境: [root@localhost ~]# cat /etc/RedHat-release CentOS release 6.7 (Final)[root@localhost tools]# ...

  9. luogu T96516 [DBOI2019]持盾 可持久化线段树+查分

    因为题中的操作是区间加法,所以满足前缀相减性. 而每一次查询的时候还是单点查询,所以直接用可持久化线段树维护差分数组,然后查一个前缀和就行了. code: #include <bits/stdc ...

  10. SVN “Previous operation has not finished”

    https://jingyan.baidu.com/article/cbcede0761334902f40b4d31.html 需要运行sqlite3打开.svn下的wc.db数据库文件, sqlit ...