Design Add and Search Words Data Structure

MediumString

Description

Design a data structure that supports adding new words and finding if a string matches any previously added string. '.' can match any letter. Return true or false.

Examples

Input:addWord("bad"), search("b.d")
Output:true
Explanation:

After adding 'bad', searching 'b.d' returns true because '.' is a wildcard matching any character, here 'a'.

Input:input = ["bad","dad","mad"], arg2 = "pad"
Output:false
Explanation:

The target value does not exist in the given data structure.

Input:input = ["bad","dad","mad"], arg2 = "b.d"
Output:true
Explanation:

Pattern 'b.d' matches 'bad' where '.' stands for 'a'.

Input:input = ["bad","dad","mad"], arg2 = ".ad"
Output:true
Explanation:

Pattern '.ad' matches all three words ('bad', 'dad', 'mad') since '.' can be 'b', 'd', or 'm'.

Constraints

  • 1 ≤ word.length ≤ 25
  • word consists of lowercase English letters.
  • search word may contain '.'.

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!