zoukankan      html  css  js  c++  java
  • 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
                }
            }
        }
    }
  • 相关阅读:
    小菜读书---《程序员修炼之道–从小工到专家》
    小菜读书--《大话设计模式》
    darknet-mini:带注释的darknet简化版,助你深入理解YOLO
    如何使用VLC进行视频录像
    海康相机打开的方法
    YOLOv5训练自己的数据集(超详细完整版)
    用GANs来做数据增强
    torch ----------->>>>rknn
    Linux下的tar压缩解压缩命令详解
    小缺陷目标检测网络--PCB缺陷检测—TDD-net
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/15063391.html
Copyright © 2011-2022 走看看