zoukankan      html  css  js  c++  java
  • 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  */
  • 相关阅读:
    Python: Lambda Functions
    转:解决Python2.7的UnicodeEncodeError: ‘ascii’ codec can’t encode异常错误
    Python 读取json文件
    (转)The windows boot configuration data file dose not contain a valid OS entry
    Python+PyQt5:停靠组件QDockWidget的实现
    Python零碎(一)
    (转)有关thread线程
    (转)有关Queue队列
    (转)Python中的random模块
    (转)python中的参数:*args和**kwargs
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/11627660.html
Copyright © 2011-2022 走看看