LeetCode 211. Add and Search Word - Data structure design(单词检索)

原题网址:https://leetcode.com/problems/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

Note:
You may assume that all words are consist of lowercase letters a-z.

click to show hint.

You should be familiar with how a Trie works. If not, please work on this problem:  Implement Trie (Prefix Tree) first.

方法一:前缀树+广度优先搜索。

public class WordDictionary {
    
    private TrieNode root = new TrieNode();

    // Adds a word into the data structure.
    public void addWord(String word) {
        char[] wa = word.toCharArray();
        TrieNode node = root;
        for(int i=0; i<wa.length; i++) node = node.append(wa[i]);
        node.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) return false;
        char[] wa = word.toCharArray();
        LinkedList<State> queue = new LinkedList<>();
        queue.add(new State(root, 0));
        while (!queue.isEmpty()) {
            State state = queue.remove();
            if (state.pos < wa.length) {
                if (wa[state.pos] == '.') {
                    for(int i=0; i<26; i++) {
                        if (state.node.nexts[i] != null) {
                            queue.add(new State(state.node.nexts[i], state.pos+1));
                        }
                    }
                } else if (state.node.nexts[wa[state.pos]-'a'] != null) {
                    queue.add(new State(state.node.nexts[wa[state.pos]-'a'], state.pos+1));
                }
            } else if (state.node.isWord) return true;
        }
        return false;
    }
}
class TrieNode {
    boolean isWord;
    TrieNode[] nexts = new TrieNode[26];
    TrieNode append(char ch) {
        if (nexts[ch-'a']!=null) return nexts[ch-'a'];
        nexts[ch-'a'] = new TrieNode();
        return nexts[ch-'a'];
    }
}
class State {
    TrieNode node;
    int pos;
    State(TrieNode node, int pos) {
        this.node = node;
        this.pos = pos;
    }
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");

方法二:前缀树+深度优先搜索。

public class WordDictionary {
    
    private TrieNode root = new TrieNode();
    
    private boolean find(char[] wa, int pos, TrieNode node) {
        if (pos == wa.length) return node.isWord;
        if (wa[pos] == '.') {
            for(int i=0; i<26; i++) {
                if (node.nexts[i] != null && find(wa, pos+1, node.nexts[i])) return true;
            }
        } else return node.nexts[wa[pos]-'a'] != null && find(wa, pos+1, node.nexts[wa[pos]-'a']);
        return false;
    }

    // Adds a word into the data structure.
    public void addWord(String word) {
        char[] wa = word.toCharArray();
        TrieNode node = root;
        for(int i=0; i<wa.length; i++) node = node.append(wa[i]);
        node.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) return false;
        char[] wa = word.toCharArray();
        return find(wa, 0, root);
    }
}
class TrieNode {
    boolean isWord;
    TrieNode[] nexts = new TrieNode[26];
    TrieNode append(char ch) {
        if (nexts[ch-'a']!=null) return nexts[ch-'a'];
        nexts[ch-'a'] = new TrieNode();
        return nexts[ch-'a'];
    }
}
// Your WordDictionary object will be instantiated and called as such:
// WordDictionary wordDictionary = new WordDictionary();
// wordDictionary.addWord("word");
// wordDictionary.search("pattern");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值