211. Design Add and Search Words Data Structure

Design Add and Search Words Data Structure (LeetCode 211) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.

How to solve Design Add and Search Words Data Structure

Design Add and Search Words Data Structure is a Data Structure Design problem, part of Design Patterns. Design and implement data structures: LRU cache, LFU cache, HashMap, and specialized structures.

Work towards the solution in these steps:

  1. You should be familiar with how a Trie works. If not, please work on this problem: <a href="https://leetcode.com/problems/implement-trie-prefix-tree/">Implement Trie (Prefix Tree)</a> first.

The reference solution runs in O(L) time and O(N * L) space.

The Design Add and Search Words Data Structure solution and editorial walks through the full approach with reference code.

Problem statement

Design a data structure that supports adding new words and finding if a string matches any previously added string.

Implement the WordDictionary class:

- WordDictionary() Initializes the object.

- void addWord(word) Adds word to the data structure, it can be matched later.

- bool search(word) Returns true if there is any string in the data structure that matches word or false otherwise. word may contain dots '.' where dots can be matched with any letter.

Example:

Input ["WordDictionary","addWord","addWord","addWord","search","search","search","search"] [[],["bad"],["dad"],["mad"],["pad"],["bad"],[".ad"],["b.."]] Output [null,null,null,null,false,true,true,true]

Explanation WordDictionary wordDictionary = new WordDictionary(); wordDictionary.addWord("bad"); wordDictionary.addWord("dad"); wordDictionary.addWord("mad"); wordDictionary.search("pad"); // return False wordDictionary.search("bad"); // return True wordDictionary.search(".ad"); // return True wordDictionary.search("b.."); // return True

Constraints:

- 1 <= word.length <= 25

- word in addWord consists of lowercase English letters.

- word in search consist of '.' or lowercase English letters.

- There will be at most 2 dots in word for search queries.

- At most 10^4 calls will be made to addWord and search.

Topics and companies

Topics: String, Depth-First Search, Design, Trie.

Reported in interviews at Facebook, Google, Oracle, Amazon.

How to practise Design Add and Search Words Data Structure on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(L) time and O(N * L) space.

Open Design Add and Search Words Data Structure in the code editor, or read the Design Add and Search Words Data Structure editorial for a worked solution with its approach and complexity analysis.

Where Design Add and Search Words Data Structure sits in the DSA pattern sheet

Problems related to Design Add and Search Words Data Structure

Other problems that use the same Design (General/Specific) and Design Patterns techniques:

Design Add and Search Words Data Structure FAQ

What is the LeetCode number of Design Add and Search Words Data Structure?

Design Add and Search Words Data Structure is LeetCode problem 211, rated Medium.

What pattern is used to solve Design Add and Search Words Data Structure?

Design Add and Search Words Data Structure is a Data Structure Design problem. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.

What is the time complexity of Design Add and Search Words Data Structure?

The reference solution runs in O(L) time and O(N * L) space.

Which companies ask Design Add and Search Words Data Structure?

Design Add and Search Words Data Structure has been reported in interviews at Facebook, Google, Oracle, Amazon.

Preparing this workspace.