• Daily Coding Problem: Problem #949


    /**
     * This problem was asked by Twitter.
    Implement an autocomplete system.
    That is, given a query string s and a set of all possible query strings, return all strings in the set that have s as a prefix.
    For example, given the query string de and the set of strings [dog, deer, deal], return [deer, deal].
    Hint: Try preprocessing the dictionary into a more efficient data structure to speed up queries.
     * */
    class Problem_949 {
    
        /*
        * solution: DFS and Trie Tree(prefix tree), add all word into prefix tree for searching;
        * Time and Space complexity are: O(2^L), L is the length of key word
        * */
    
        private var root: TrieNode? = null
    
        init {
            root = TrieNode()
        }
    
        fun getSamePrefix(query: String, strings: ArrayList<String>): ArrayList<String> {
            val result = ArrayList<String>()
            for (word in strings) {
                root?.insert(word)
            }
            var lastNode = root
            val sb = StringBuilder()
            //find out the last node for start dfs
            for (c in query) {
                lastNode = lastNode?.children?.get(c)
                if (lastNode != null) {
                    sb.append(c)
                }
            }
            dfs(lastNode, result, sb)
            return result
        }
    
        private fun dfs(node: TrieNode?, list: ArrayList<String>, cur: StringBuilder) {
            if (node?.isWord!!) {
                list.add(cur.toString())
            }
            if (node.children.isEmpty()) {
                return
            }
            for (child in node.children.values) {
                dfs(child, list, cur.append(child.char.toString()))
                //reduce the length for backtracking
                cur.setLength(cur.length - 1)
            }
        }
    
        /**
         * node for TrieTree
         * */
        class TrieNode constructor(c: Char?) {
            var isWord = false
            var char: Char? = null
            val children = HashMap<Char, TrieNode>()
    
            constructor() : this(null)
    
            init {
                char = c
            }
    
            fun insert(word: String?) {
                if (word == null || word.isEmpty()) {
                    return
                }
                val first = word[0]
                var child = children.get(first)
                if (child == null) {
                    child = TrieNode(first)
                    children.put(first, child)
                }
                //insert entire word
                if (word.length > 1) {
                    child.insert(word.substring(1))
                } else {
                    child.isWord = true
                }
            }
        }
    }
  • 相关阅读:
    如何编译树莓派内核
    代码导出Reporting Services报表文件
    Bit-Coin收入的一分钱
    如何在树莓派上运行雷神之锤III
    新树莓派入手
    如何通过PowerShell在Visual Studio的Post-build中预热SharePoint站点
    每日一题20201218(389. 找不同)
    每日一题20201217(714. 买卖股票的最佳时机含手续费)
    每日一题20201216(290. 单词规律)
    每日一题20201215(738. 单调递增的数字)
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/15063391.html
Copyright © 2020-2023  润新知