• 211. Add and Search Word


    package LeetCode_211
    
    /**
     * 211. Add and Search Word - Data structure design
     * https://leetcode.com/problems/add-and-search-word-data-structure-design/description/
     *
     * Design a data structure that supports the following two operations:
    =void addWord(word)
    =bool search(word)
    search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.
    
    Example:
    addWord("bad")
    addWord("dad")
    addWord("mad")
    search("pad") -> false
    search("bad") -> true
    search(".ad") -> true
    search("b..") -> true
    
    Note:
    You may assume that all words are consist of lowercase letters a-z.
     * */
    
    class Trie {
        var end = false
        var children = arrayOfNulls<Trie>(26)
    }
    
    class WordDictionary() {
        /*
        * solution: Trie Tree,
        * */
    
        var root: Trie? = null
    
        /** Initialize your data structure here. */
        init {
            root = Trie()
        }
    
        /** Adds a word into the data structure. */
        fun addWord(word: String) {
            var node = root
            //while (k < word.length) {
            for (ch in word) {
                val pos = ch - 'a'
                if (node?.children!![pos] == null) {
                    node.children!![pos] = Trie()
                }
                node = node.children!![pos]
            }
            node?.end = true
        }
    
        /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
        fun search(word: String): Boolean {
            return dfs(word, 0, root)
        }
    
        private fun dfs(word: String, pos: Int, root: Trie?): Boolean {
            if (root == null) {
                return false
            }
            if (pos == word.length) {
                return root.end
            }
            val ch = word[pos]
            println("ch:$ch")
            if (ch == '.') {
                //if meet '.', need search all children
                for (child in root.children!!) {
                    if (child != null && dfs(word, pos + 1, child)) {
                        return true
                    }
                }
            } else {
                return dfs(word, pos + 1, root.children!![ch - 'a'])
            }
            return false
        }
    }
    
    /**
     * Your WordDictionary object will be instantiated and called as such:
     * var obj = WordDictionary()
     * obj.addWord(word)
     * var param_2 = obj.search(word)
     */
  • 相关阅读:
    sql server sa 用户 属性
    关于SQL执行顺序
    SQL server 数据库版本查询
    版本生成|Ext form输入框后加文字说明
    继承 tabpanel 对父类重新赋值 父类方法重写
    Ext.namespace() Ext命名空间介绍
    Ext.apply Ext.applyif 的理解
    下拉列表
    extjs表单FormPanel验证
    提高篇:第十三篇
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13352639.html
Copyright © 2020-2023  润新知