[LeetCode]Add and Search Word - Data structure design,解题报告

目录


思路

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.

For example:

addWord(“bad”)
addWord(“dad”)
addWord(“mad”)
search(“pad”) -> false
search(“bad”) -> true
search(“.ad”) -> true
search(“b..”) -> true


思路

因为之前做了一道字典树的题目,这道题目需要借鉴字典树的数据结构。点击进入字典树参考链接

具体思路如下:

  1. 用Trie树来存储插入的字符串。
  2. 用DFS来搜索Trie树中的字符串。

为什么这里只能用DFS而不能用BFS呢?

是因为:BFS无法保证下一层查找的结点是否属于当前的父节点。


AC代码

有了字典树的基础,我直接上AC代码了,大家可以通过代码学习具体的思路:

import java.util.HashMap;

class WordTrieNode {
    boolean isWord;
    int index;
    HashMap<Character, WordTrieNode> nexts;

    public WordTrieNode() {
        nexts = new HashMap<Character, WordTrieNode>();
    }
}

public class WordDictionary {
    private WordTrieNode root;

    public WordDictionary() {
        root = new WordTrieNode();
    }

    // Adds a word into the data structure.
    public void addWord(String word) {
        WordTrieNode p = root;
        int i = 0, len = word.length();

        // traverse existing
        while (i < len) {
            char ch = word.charAt(i);
            if (p.nexts.containsKey(ch)) {
                p = p.nexts.get(ch);
                i ++;
            } else {
                break;
            }
        }

        // append new word
        while (i < len) {
            WordTrieNode newNode = new WordTrieNode();
            newNode.index = i;
            p.nexts.put(word.charAt(i), newNode);
            p = newNode;
            i ++;
        }

        // set word end
        p.isWord = true;
    }

    // Returns if the word is in the data structure. A word could
    // contain the dot character '.' to represent any one letter.
    public boolean search(String word) {
        if (word == null || word.length() == 0) {
            return false;
        }

        WordTrieNode p = root;
        return dfs(word, 0, p);
    }   

    private boolean dfs(String word, int index, WordTrieNode p) {
        if (index == word.length() - 1) {
            if (word.charAt(index) == '.') {
                for (WordTrieNode node : p.nexts.values()) {
                    if (node.isWord) {
                        return true;
                    }
                }
                return false;
            } else {
                WordTrieNode endNode = p.nexts.get(word.charAt(index));
                return endNode != null && endNode.isWord;
            }
        }

        if (index >= word.length()) {
            return false;
        }

        if (word.charAt(index) == '.') {
            boolean res = false;
            for (WordTrieNode node : p.nexts.values()) {
                res |= dfs(word, index + 1, node);
            }
            return res;
        } else {
            if (p.nexts.containsKey(word.charAt(index))) {
                return dfs(word, index + 1, p.nexts.get(word.charAt(index)));
            } else {
                return false;
            }
        }
    }
}

// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值