LeetCode 1032. Stream of Characters

原题链接在这里:https://leetcode.com/problems/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.

题解:

Could use Trie.

For current char, we could maintain a list of previous nodes. These nodes represent stop positions of previous chars.

Then for each node, see if with this char, if we could make any words and update list.

Time Complexity: StreamChecker, O(m * n). m = words.length. n is length of word. query, O(k). k is prevous entered char count.

Space: O(m*n + k ^ 2). Trie + list size.

AC Java:

 1 class StreamChecker {
 2     TrieNode root;
 3     List<TrieNode> list;
 4     
 5     public StreamChecker(String[] words) {
 6         root = new TrieNode();
 7         for(String word : words){
 8             TrieNode p = root;
 9             for(char c : word.toCharArray()){
10                 if(p.nexts[c - 'a'] == null){
11                     p.nexts[c - 'a'] = new TrieNode();
12                 }
13                 
14                 p = p.nexts[c - 'a'];
15             }
16             
17             p.val = word;
18         }
19         
20         list = new ArrayList<>();
21         list.add(root);
22     }
23     
24     public boolean query(char letter) {
25         boolean res = false;
26         List<TrieNode> temp = new ArrayList<>();
27         temp.add(root);
28         
29         for(TrieNode p : list){
30             if(p != null && p.nexts[letter - 'a'] != null){
31                 p = p.nexts[letter - 'a'];
32                 if(p.val != null){
33                     res = true;
34                 }
35                 
36                 temp.add(p);
37             }
38         }
39         
40         list = temp;
41         return res;
42     }
43 }
44 
45 class TrieNode{
46     TrieNode [] nexts;
47     String val;
48     public TrieNode(){
49         nexts = new TrieNode[26];
50     }
51 }
52 
53 /**
54  * Your StreamChecker object will be instantiated and called as such:
55  * StreamChecker obj = new StreamChecker(words);
56  * boolean param_1 = obj.query(letter);
57  */

We could build Trie with reverse word. And have a StringBuilder sb to maintain previous letters.

For new letter, from back to head of sb, check if there would be a word.

Time Complexity: StreamChecker, O(m * n). m = words.length. n is length of word. query, O(k). k is prevous entered char count.

Space: O(m*n + k). Trie + list size.

AC Java:

 1 class StreamChecker {
 2     TrieNode root;
 3     StringBuilder sb;
 4     
 5     public StreamChecker(String[] words) {
 6         root = new TrieNode();
 7         sb = new StringBuilder();
 8         
 9         for(String word : words){
10             TrieNode p = root;
11             for(int i = word.length() - 1; i >= 0; i--){
12                 char c = word.charAt(i);
13                 if(p.nexts[c - 'a'] == null){
14                     p.nexts[c - 'a'] = new TrieNode();
15                 }
16                 
17                 p = p.nexts[c - 'a'];
18             }
19             
20             p.isWord = true;
21         }
22     }
23     
24     public boolean query(char letter) {
25         sb.append(letter);
26         TrieNode p = root;
27         for(int i = sb.length() - 1; i >= 0; i--){
28             char c = sb.charAt(i);
29             if(p.nexts[c - 'a'] == null){
30                 return false;
31             }
32             
33             p = p.nexts[c - 'a'];
34             if(p.isWord){
35                 return true;
36             }
37         }
38         
39         return false;
40     }
41 }
42 
43 class TrieNode{
44     TrieNode [] nexts;
45     boolean isWord;
46     public TrieNode(){
47         nexts = new TrieNode[26];
48     }
49 }
50 
51 /**
52  * Your StreamChecker object will be instantiated and called as such:
53  * StreamChecker obj = new StreamChecker(words);
54  * boolean param_1 = obj.query(letter);
55  */
原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12132221.html