# Add and Search Word with Regular Expression - Data structure design

> Add and Search Word with Regular Expression - 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 . or \* . A . means it can represent any one letter. A \* can be any sequence of characters (including the empty sequence).
>
> For example:
>
> addWord("bad")
>
> addWord("dad")
>
> addWord("mad")
>
> search("pad") -> false
>
> search("bad") -> true
>
> search(".ad") -> true
>
> search("b..") -> true

## Thoughts

empty意味着i+1但node不往后走, any other sequence意味着node可以跳过任意次, 再和i + 1匹配.

## Code

```
class WordDictionary {
    class TrieNode {
        TrieNode[] children = new TrieNode[26];
        boolean end;
    }
    TrieNode root;
    /** Initialize your data structure here. */
    public WordDictionary() {
        root = new TrieNode();
    }

    /** Adds a word into the data structure. */
    public void addWord(String word) {
        TrieNode node = root;
        for (int i = 0; i < word.length(); i++) {
            int index = word.charAt(i) - 'a';
            if (node.children[index] == null) {
                node.children[index] = new TrieNode();
            }
            node = node.children[index];
        }
        node.end = true;
    }

    private boolean dfs(String word, TrieNode node, int pos) {
        for (int i = pos; i < word.length(); i++) {
            if (word.charAt(i) == '.') {
                for (int j = 0; j < 26; j++) {
                    if (node.children[j] != null) {
                        if (dfs(word, node.children[j], i + 1)) {
                            return true;
                        }
                    }
                }
                return false;
            } else if (word.charAt(i) == '*') {
                // be any seq of characters, including empty
                for (int j = 0; j < 26; j++) {
                    if (node.children[j] != null) {
                        if (dfs(word, node.children[j], i) // skip nodes: acccb(node), a*(pos)b skip ccc
                            || dfs(word, node.children[j], i + 1) // jump to next character, a*b(pos), acccb(node)
                            || dfs(word, node, i + 1)) { // skip *: empty a(node)b, a*(pos)b => a(node)b, a*b(pos)
                            return true;
                        } 
                    }
                }
                return false;
            } else {
                int index = word.charAt(i) - 'a';
                if (node.children[index] == null) {
                    return false;
                }
                node = node.children[index];
            }
        }
        return node.end;
    }

    /** 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) {
        return dfs(word, root, 0);
    }
}

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

## Analysis

时间复杂度指数级.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hao-fu-1.gitbook.io/oj/trie/add-and-search-word-data-structure-design/add-and-search-word-with-regular-expression-data-structure-design.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
