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

Analysis

时间复杂度指数级.

Last updated

Was this helpful?