LeetCode--211--medium--WordDictionary

本文介绍了一种使用Trie数据结构实现单词字典的设计方案,支持单词的添加与搜索功能,其中搜索功能能处理包含字母a-z或正则表达式.的数据结构设计,用于匹配任意单个字符。
摘要由CSDN通过智能技术生成

summary:

construct trie | dfs | classify and discuss

package com.odyssey.app.algorithm.lc.trie;

/**
 * @author Dingsheng Huang
 * @date 2020/3/18 0:09
 *
 * 211
 * medium
 * 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.
 *
 * 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 WordDictionary {

    class TrieNode {
        String word = "";
        TrieNode[] children = new TrieNode[26];
    }

    TrieNode root = new TrieNode();


    /** Initialize your data structure here. */
    public WordDictionary() {

    }

    /** Adds a word into the data structure. */
    public void addWord(String word) {
        char[] chs = word.toCharArray();
        TrieNode currNode = this.root;
        for (int i = 0; i < chs.length; i++) {
            int idx = chs[i] - 'a';
            if (currNode.children[idx] == null) {
                currNode.children[idx] = new TrieNode();
            }
            currNode = currNode.children[idx];
        }
        currNode.word = word;
    }

    private boolean res = false;
    /** 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) {
        res = false;
        dfs(this.root, word.toCharArray(), 0);
        return res;
    }

    private void dfs(TrieNode currNode, char[] chs, int idx) {
        if (currNode == null || idx == chs.length) {
            return;
        }
        // classify
        if (chs[idx] == '.') {
            if (idx == chs.length - 1) {
                // todo optimize
                for (TrieNode node : currNode.children) {
                    if (node != null && node.word.length() > 0) {
                        res = true;
                        break;
                    }
                }
            } else {
                for (TrieNode node : currNode.children) {
                    dfs(node, chs, idx + 1);
                }
            }
        } else {
            if (currNode.children[chs[idx] - 'a'] != null) {
                if (idx == chs.length - 1) {
                    if (currNode.children[chs[idx] - 'a'].word.length() > 0) {
                        res = true;
                    }
                }
                dfs(currNode.children[chs[idx] - 'a'], chs, idx + 1);
            }
        }

    }
}

/**
 * Your WordDictionary object will be instantiated and called as such:
 * WordDictionary obj = new WordDictionary();
 * obj.addWord(word);
 * boolean param_2 = obj.search(word);
 */

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值