LeetCode 1032. Stream of Characters

Problem Description: 

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.

题解:

看到这个题很快想到了该用前缀树trie来存下单词表,但是此题因为是要判断以query传入的当前字母结尾的单词是否出现在单词表中,要倒序存。

而此题的关键也是倒序存和倒序的查找,在trie的基础上稍微修改代码即可完成。

时间复杂度:O(L * Q)

其中L是最大单词长度,Q是query的次数。

代码如下

 1 class StreamChecker {
 2     class Node{
 3         Node[] children;
 4         boolean endOfWord;
 5         public Node() {
 6             children = new Node[26];
 7             endOfWord = false;
 8         }
 9     }
10     
11     Node root;
12     StringBuilder sb;
13     
14     private void insert(String word) {
15         Node cur = root;
16         for(int i = word.length() - 1; i >= 0; i--) {
17             char ch = word.charAt(i);
18             Node next = cur.children[ch - 'a'];
19             if(next == null) {
20                 cur.children[ch - 'a'] = next = new Node();
21             }
22             cur = next;
23         }
24         cur.endOfWord = true;
25     }
26     
27     public StreamChecker(String[] words) {
28         root = new Node();
29         sb = new StringBuilder();
30         for(String word : words) {
31             insert(word);
32         }
33     }
34     
35     public boolean query(char letter) {
36         boolean res = false;
37         
38         sb.append(letter);
39         Node cur = root;
40         for(int i = sb.length() - 1; i >= 0; i--) {
41             char ch = sb.charAt(i);
42             if(cur.children[ch - 'a'] == null) {
43                 return false;
44             }
45             cur = cur.children[ch - 'a'];
46             if(cur.endOfWord) return true;
47         }
48 
49         return false;
50     }
51 }

 

posted @ 2019-04-21 20:25  起点菜鸟  阅读(828)  评论(0编辑  收藏  举报