zoukankan      html  css  js  c++  java
  • 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  */
  • 相关阅读:
    《Hadoop应用开发技术详解》
    c#中WMI 中的日期和时间转为本地时间
    c# 获取某个进程的CPU使用百分百(类似任务管理器中显示CPU)
    获得特定进程信息
    javascript利用jquery-1.7.1来判断是否是谷歌Chrome浏览器
    Oracle 用Drapper进行like模糊传参查询需要在参数值前后带%符合
    IIS7.0部署MVC/WebApi项目,报404.4错误
    MVC+EF6+Oracle,提示ORA-01918: user '***' does not exist
    MongoDB查询转对象是出错Element '_id' does not match any field or property of class
    MongoDB 导出、导入表
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12132221.html
Copyright © 2011-2022 走看看