The problem “Add and Search Word – Data structure design LeetCode” asks us to create or design a new data structure. Such that which can be used for adding or storing a word and searching the words where the search function can search even a regular expression from the word.
For instance :
If string/word = “hey” is stored in the map then the following functions will give the same output –
1. search (..y).
2. search (.e.).
3. search (h..).
All the above three function calls will result in the output to be true.
Table of Contents
Example
addword ("code") addword ("java") addword ("when") search ("blue") search ("java") search ("co..")
False True True
addword ("who") addword ("why") search ("why") search ("hey") search ("the")
True False False
Using Resizable array and Hashmap
Algorithm
- Initialize a class for the new data structure.
- After that, initialize a resizable array of string type and a hashmap of the type of the pair of the string and the integer type.
- Create a function to add the word in the new data structure which accepts a string variable as its parameter.
- After that, check if the given string is already present in the map, return.
- Else insert the given string at the last index of the array and in the map also.
- Similarly, create another function to search the word in the new data structure which accepts a string variable as its parameter.
- Search the given string variable on the map.
- If the string variable is present in the map print “True”.
- Else check the regular expression. If it is found print “True”.
- Print “False”.
Implementation
C++ code for Add and Search Word – Data structure design LeetCode
#include<bits/stdc++.h> using namespace std; class newStructure{ vector <string> arr; map <string, int> Map; public: void addword(string x){ if(Map.find(x) != Map.end()) return; int index = arr.size(); arr.push_back(x); Map.insert(std::pair<string,int>(x, index)); } void search(string x){ if(Map.find(x) != Map.end()){ cout<<"True"<<endl; return; } else{ regex b(x); for(int i=0; i<arr.size(); i++){ if(regex_match(arr[i],b)){ cout<<"True"<<endl; return; } } } cout<<"False"<<endl; } }; int main(){ newStructure ds; ds.addword("code"); ds.addword("java"); ds.addword("when"); ds.search("blue"); ds.search("java"); ds.search("co.."); return 0; }
False True True
Java code for Add and Search Word – Data structure design LeetCode
import java.util.*; import java.util.regex.Pattern; class newStructure{ ArrayList<String> arr; HashMap<String, Integer> map; public newStructure(){ arr = new ArrayList<String>(); map = new HashMap<String, Integer>(); } void addword(String x){ if(map.get(x) != null) return; int s = arr.size(); arr.add(x); map.put(x, s); } void search(String x){ if(map.get(x)!=null){ System.out.println("True"); return; } else{ Pattern regex = Pattern.compile(x); for(String s:arr){ if(regex.matcher(s).matches()){ System.out.println("True"); return; } } } System.out.println("False"); } } class Main{ public static void main (String[] args){ newStructure ds = new newStructure(); ds.addword("code"); ds.addword("java"); ds.addword("when"); ds.search("blue"); ds.search("java"); ds.search("co.."); } }
False True True
Complexity Analysis for Add and Search Word – Data structure design LeetCode
Time Complexity
O(n*m) where n is the number of words added and m is the length of the words to be searched.
Space Complexity
O(n) because this program uses space for n elements in the resizable array and hashmap, therefore the space complexity is O(n).