The Question
Design a data structure that supports the following two operations:
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
You can practice it on leetcode before reading the solution
Solution
Requires making us a tries for the inserted string
Then we search the tries but when we encounter a ‘.’ we try all 26 possibilities and move the index to the next word;
I am using a recursive solution as it will make the solution code simpler to understand/debug which has an advantage in interviews and in the work
Code In Java
Theoretical
Insert Time Complexity: O(N)
Search Time Complexity: O(M + (N-M)26)
M is the number of . in the search
Leetcode
Memory: 50.5 MB
Runtime: 80 ms
Evaluation
Time: 42.06%
Space: 100.00%