Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Adding and searching for words in custom Data Structure in JavaScript
We are required to design a data structure in JavaScript that supports two key operations: adding words and searching with pattern matching using regular expressions.
Problem
The data structure must support the following operations:
- addWord - adds a word to the data structure using arrays or any other storage mechanism
- search - searches for a literal word or a regular expression pattern containing lowercase letters "a-z" or "." where "." can represent any single letter
Example Requirements
addWord("sir")
addWord("car")
addWord("mad")
search("hell") === false
search(".ad") === true
search("s..") === true
Implementation
Here's a complete implementation using a class-based approach with an internal array to store words:
class MyData{
constructor(){
this.arr = [];
}
}
MyData.prototype.addWord = function (word) {
this.arr.push(word);
};
MyData.prototype.search = function (word) {
let reg = new RegExp('^' + word + '$');
return !!this.arr.find(el => reg.test(el));
};
const data = new MyData();
data.addWord('sir');
data.addWord('car');
data.addWord('mad');
console.log(data.search('hell')); // false - word not found
console.log(data.search('.ad')); // true - matches "mad"
console.log(data.search('s..')); // true - matches "sir"
false true true
How It Works
The implementation uses these key concepts:
- Array Storage - Words are stored in a simple array for easy iteration
- Regular Expression - The search pattern is converted to a RegExp with anchors (^ and $) to match the entire word
- Pattern Matching - The dot (.) character acts as a wildcard matching any single character
- Boolean Conversion - The double negation (!!) converts the find result to a boolean
Alternative ES6 Implementation
Here's a more modern approach using ES6 class syntax:
class WordDataStructure {
constructor() {
this.words = [];
}
addWord(word) {
this.words.push(word);
}
search(pattern) {
const regex = new RegExp('^' + pattern + '$');
return this.words.some(word => regex.test(word));
}
}
const wordDS = new WordDataStructure();
wordDS.addWord('cat');
wordDS.addWord('bat');
wordDS.addWord('rat');
console.log(wordDS.search('cat')); // true
console.log(wordDS.search('.at')); // true - matches all three
console.log(wordDS.search('dog')); // false
true true false
Key Points
- The caret (^) and dollar ($) anchors ensure exact word matching
- The dot (.) character matches any single lowercase letter
- Using
some()method provides better performance thanfind()for boolean results - Time complexity: O(n) for search operations where n is the number of stored words
Conclusion
This data structure efficiently combines array storage with regular expression pattern matching. The implementation supports both exact word searches and flexible pattern matching using wildcards.
Advertisements
