zoukankan      html  css  js  c++  java
  • 1032. 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

    class StreamChecker {
        class TrieNode {
            TrieNode[] children = new TrieNode[26];
            boolean isWord;
        }
    
        TrieNode root;
        StringBuilder sb;
    
        public StreamChecker(String[] words) {
            root = new TrieNode();
            sb = new StringBuilder();
            buildTrie(words);
        }
    
        private void buildTrie(String[] words) {
            for(String w : words) {
                TrieNode cur = root;
                for(int i = w.length() - 1; i >= 0; i--) {
                    if(cur.children[w.charAt(i) - 'a'] == null) {
                        cur.children[w.charAt(i) - 'a'] = new TrieNode();
                    }
                    cur = cur.children[w.charAt(i) - 'a'];
                }
                cur.isWord = true;
            }
        }
    
        public boolean query(char letter) {
            sb.append(letter);
            TrieNode cur = root;
            for(int i = sb.length() - 1; i >= 0 && cur != null; i--) {
                cur = cur.children[sb.charAt(i) - 'a'];
                if(cur != null && cur.isWord) {
                    return true;
                }
            }
            return false;
        }
    }
    
    /**
     * Your StreamChecker object will be instantiated and called as such:
     * StreamChecker obj = new StreamChecker(words);
     * boolean param_1 = obj.query(letter);
     */
  • 相关阅读:
    使用metasploit进行栈溢出攻击-2
    使用metasploit进行栈溢出攻击-1
    如何利用 Visual Studio 自带工具提高开发效率
    Visaul Studio 常用快捷键动画演示
    WinForm AutoComplete 输入提示、自动补全
    RTL2832U+R820T电视棒windows下安装sdr# 以及搭建ADS-B使用VirtualRadar看飞机的教程
    传说中的WCF(12):服务器回调有啥用
    传说中的WCF(11):会话(Session)
    传说中的WCF(10):消息拦截与篡改
    传说中的WCF(9):流与文件传输
  • 原文地址:https://www.cnblogs.com/fatttcat/p/13601298.html
Copyright © 2011-2022 走看看