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  */
  • 相关阅读:
    RMAN备份脚本
    配置putty隧道登录远程window远程或远程数据库
    ssh登录报错:no common kex alg
    Solaris上修改进程能够打开的最大文件句柄数
    如何在V890上安装配置rsc(转)
    Oracle表或分区导出脚本
    检查日期合法性脚本(转)
    Solaris下常用的磁带操作命令
    如何从solaris操作系统上获取机器的sn号
    如何修复failed磁盘和"DISABLED RECOVER"状态的plex
  • 原文地址:https://www.cnblogs.com/EdwardLiu/p/11627660.html
Copyright © 2011-2022 走看看