DIY: Design Add and Search Words Data Structure
Solve the interview question "Design Add and Search Words Data Structure" in this lesson.
We'll cover the following
Problem statement
Design a data structure that supports the following functions:
- Adding new words.
- Finding if a string matches any previously added string.
- Returning all the words that are present in the data structure.
Let’s call this data structure the WordDictionary
module. Here is how it should be implemented:
init
: This function will initialize the object.add_word(obj, word)
: This function will addword
to the data structure so that it can be matched later.search(obj, word)
: This function will returntrue
if there is any string in theWordDictionary
object that matches the queryword
. Otherwise, it will returnfalse
. If the queryword
contains dots.
, then it should be matched with every letter. For example:.
in the string".ad"
can have 26 possible search results like“aad”
,“bad”
,“cad”
, and so on.get_words(obj)
: This function will return all of the words present in theWordDictionary
module.
Note: You cannot add a word that is already present in the dictionary.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.