• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅

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

 

 1 class StreamChecker {
 2     
 3     TrieNode root;
 4     StringBuilder sb;
 5 
 6     public StreamChecker(String[] words) {
 7         this.root = new TrieNode();
 8         this.sb = new StringBuilder();
 9         
10         for (String word : words) {
11             TrieNode node = root;
12             for (int i = word.length() - 1; i >= 0; i --) {
13                 if (node.children[word.charAt(i) - 'a'] == null) {
14                     node.children[word.charAt(i) - 'a'] = new TrieNode();
15                 }
16                 node = node.children[word.charAt(i) - 'a'];
17             }
18             node.isWord = true;
19         }
20     }
21     
22     public boolean query(char letter) {
23         sb.append(letter);
24         TrieNode node = root;
25         for (int i = sb.length() - 1; i >= 0 && node != null; i --) {
26             node = node.children[sb.charAt(i) - 'a'];
27             if (node != null && node.isWord) return true;
28         }
29         return false;
30     }
31     
32     class TrieNode {
33         TrieNode[] children;
34         boolean isWord;
35         public TrieNode() {
36             this.children = new TrieNode[26];
37             this.isWord = false;
38         }
39     }
40 }
41 
42 /**
43  * Your StreamChecker object will be instantiated and called as such:
44  * StreamChecker obj = new StreamChecker(words);
45  * boolean param_1 = obj.query(letter);
46  */

 

posted @ 2019-10-06 16:38  neverlandly  阅读(359)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3