leetcode刷题笔记 211题 添加与搜索单词

leetcode刷题笔记 211题 添加与搜索单词 - 数据结构设计

源地址:211. 添加与搜索单词 - 数据结构设计

问题描述:

如果数据结构中有任何与word匹配的字符串,则bool search(word)返回true,否则返回false。 单词可能包含点“。” 点可以与任何字母匹配的地方。

请你设计一个数据结构,支持 添加新单词 和 查找字符串是否与任何先前添加的字符串匹配 。

实现词典类 WordDictionary :

WordDictionary() 初始化词典对象
void addWord(word) 将 word 添加到数据结构中,之后可以对它进行匹配
bool search(word) 如果数据结构中存在字符串与 word 匹配,则返回 true ;否则,返回 false 。word 中可能包含一些 '.' ,每个 . 都可以表示任何一个字母。

示例:

输入:
["WordDictionary","addWord","addWord","addWord","search","search","search","search"]
[[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]]
输出:
[null,null,null,null,false,true,true,true]

解释:
WordDictionary wordDictionary = new WordDictionary();
wordDictionary.addWord("bad");
wordDictionary.addWord("dad");
wordDictionary.addWord("mad");
wordDictionary.search("pad"); // return False
wordDictionary.search("bad"); // return True
wordDictionary.search(".ad"); // return True
wordDictionary.search("b.."); // return True

提示:

1 <= word.length <= 500
addWord 中的 word 由小写英文字母组成
search 中的 word 由 '.' 或小写英文字母组成
最调用多 50000 次 addWord 和 search

//本题解法主要参考于208题 实现Trie数据结构
//二者主要区别在于211题引入了'.'标识符,代表任意字符
//需要对search函数进行重写对'.'的操作
class WordDictionary() {

    /** Initialize your data structure here. */
    class Node(val children: Array[Node], var isWordEnd: Boolean)
    val root = new Node(new Array[Node](26), false)
    
    /** Adds a word into the data structure. */
    def addWord(word: String) {
        var ptr = root
        for (ch <- word) {
            if (ptr.children(ch - 'a') == null) ptr.children(ch - 'a') = new Node(new Array[Node](26), false)
            ptr = ptr.children(ch - 'a')
        }
        ptr.isWordEnd = true
    }

    /** SearchHelper Func **/
    //当当前字符为'.'时,要进一步访问所有子节点看看
    //由于主要确定单词存在即可,故使用DFS处理
    def searchHelper(word: String, ptr: Node, pos: Int): Boolean = {
        //DFS退出条件,遍历长度与单词长度一致,且为一个完整单词
        if (pos == word.length && ptr.isWordEnd == true)  return true
        //DFS退出条件,遍历长度与单词长度一致,但不满足一个单词的要求
        if (pos == word.length && ptr.isWordEnd == false) return false
        
        val ch = word.charAt(pos)
        if (ch == '.') {
            //当前字符为'.',为所有子节点进行DFS,注意排除空节点
            for (node <- ptr.children if node != null) {
                val res = searchHelper(word, node, pos+1)
                if (res == true) return res
            }
        }
        else{
            if (ptr.children(ch - 'a') != null) {
                val res = searchHelper(word, ptr.children(ch - 'a'), pos+1)
                if (res == true) return res
            }
        }
        return false
    }
    
    /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
    def search(word: String): Boolean = {
        val res = searchHelper(word, root, 0)
        return res 
    }
}

/**
 * Your WordDictionary object will be instantiated and called as such:
 * var obj = new WordDictionary()
 * obj.addWord(word)
 * var param_2 = obj.search(word)
 */
原文地址:https://www.cnblogs.com/ganshuoos/p/13745835.html