#211 Design Add and Search Words Data Structure

Description

Design a data structure that supports adding new words and finding if a string matches any previously added string.

Implement the WordDictionary class:

WordDictionary() Initializes the object.
void addWord(word) Adds word to the data structure, it can be matched later.
bool search(word) Returns true if there is any string in the data structure that matches word or false otherwise. word may contain dots ‘.’ where dots can be matched with any letter.

Examples

Example:

Input
[“WordDictionary”,“addWord”,“addWord”,“addWord”,“search”,“search”,“search”,“search”]
[[],[“bad”],[“dad”],[“mad”],[“pad”],[“bad”],[".ad"],[“b…”]]
Output
[null,null,null,null,false,true,true,true]
Explanation
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

Constraints:

1 <= word.length <= 25
word in addWord consists of lowercase English letters.
word in search consist of ‘.’ or lowercase English letters.
There will be at most 3 dots in word for search queries.
At most 1 0 4 10^4 104 calls will be made to addWord and search.

思路

我用的是前缀树,children用List来存储,但因为它只包含小写英语单词,所以可以用数组来存,这样读取的时间会更快一点

代码

class prefixTreeNode {
    public char ch;
    public List<prefixTreeNode> children;
    public boolean isEnd;
    
    public prefixTreeNode(char c) {
        this.ch = c;
        this.children = new ArrayList<>();
        this.isEnd = false;
    }
    
    public void setEnd() {
        this.isEnd = true;
    }
}
class WordDictionary {
    public prefixTreeNode root;
    
    public WordDictionary() {
        root = new prefixTreeNode('$');
    }
    
    public void addWord(String word) {
        prefixTreeNode curr = root;
        for (int i = 0; i < word.length(); i++) {
            boolean alreadyHave = false;
            for (prefixTreeNode tmp: curr.children) {
                if (tmp.ch == word.charAt(i)) {
                    alreadyHave = true;
                    curr = tmp;
                    break;
                }
            }
            if (!alreadyHave) {
                prefixTreeNode tmp = new prefixTreeNode(word.charAt(i));
                curr.children.add(tmp);
                curr = tmp;
            }
        }
        curr.setEnd();
    }
    
    public boolean search(String word) {
        return haveWord(word, 0, root);
    }
    
    public boolean haveWord(String word, int position, prefixTreeNode node) {
        if (position >= word.length())
            return node.isEnd;
        
        List<prefixTreeNode> children = node.children;
        if (word.charAt(position) != '.') {
            for (prefixTreeNode child: children) {
                if (child.ch == word.charAt(position))
                    return haveWord(word, position + 1, child);
            }
            
            return false;
        }
        
        for (prefixTreeNode child: children) {
            if (haveWord(word, position + 1, child))
                return true;
        }
        return false;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值