Skip to content

Commit ca28b6e

Browse files
committed
Add JS impl of design add and search words ds LC medium
1 parent c12cc21 commit ca28b6e

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
https://leetcode.com/problems/design-add-and-search-words-data-structure
3+
4+
Design a data structure that supports adding new words and searching for existing words.
5+
6+
Implement the WordDictionary class:
7+
8+
void addWord(word) Adds word to the data structure.
9+
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.
10+
11+
Example 1:
12+
Input:
13+
["WordDictionary", "addWord", "day", "addWord", "bay", "addWord", "may", "search", "say", "search", "day", "search", ".ay", "search", "b.."]
14+
Output:
15+
[null, null, null, null, false, true, true, true]
16+
17+
Explanation:
18+
WordDictionary wordDictionary = new WordDictionary();
19+
wordDictionary.addWord("day");
20+
wordDictionary.addWord("bay");
21+
wordDictionary.addWord("may");
22+
wordDictionary.search("say"); // return false
23+
wordDictionary.search("day"); // return true
24+
wordDictionary.search(".ay"); // return true
25+
wordDictionary.search("b.."); // return true
26+
27+
Constraints:
28+
1 <= word.length <= 20
29+
word in addWord consists of lowercase English letters.
30+
word in search consist of '.' or lowercase English letters.
31+
There will be at most 2 dots in word for search queries.
32+
At most 10,000 calls will be made to addWord and search.
33+
*/
34+
35+
class TrieNode {
36+
constructor(val) {
37+
this.val = val;
38+
this.isWord = false;
39+
this.children = {};
40+
}
41+
42+
setAsWord() {
43+
this.isWord = true;
44+
}
45+
46+
setChild(node) {
47+
this.children[node.val] = node;
48+
}
49+
50+
getChild(val) {
51+
return this.children[val];
52+
}
53+
54+
getChildren() {
55+
return this.children;
56+
}
57+
}
58+
59+
class WordDictionary {
60+
constructor() {
61+
this.root = new TrieNode("__root__");
62+
}
63+
64+
/**
65+
* @param {string} word
66+
* @return {void}
67+
*/
68+
addWord(word) {
69+
let currentNode = this.root;
70+
for (let i = 0; i < word.length; i++) {
71+
const char = word[i];
72+
73+
if (!currentNode.getChild(char)) {
74+
currentNode.setChild(new TrieNode(char));
75+
}
76+
currentNode = currentNode.getChild(char);
77+
78+
if (i === word.length - 1) {
79+
currentNode.setAsWord();
80+
}
81+
}
82+
}
83+
84+
/**
85+
* @param {string} word
86+
* @return {boolean}
87+
*/
88+
search(word) {
89+
function bfs(node, i) {
90+
if (i === word.length && node.isWord) {
91+
return true;
92+
}
93+
94+
const nodeChildren = node.getChildren();
95+
if (!Object.keys(nodeChildren).length) {
96+
return false;
97+
}
98+
99+
const char = word[i];
100+
const results = [];
101+
if (char === ".") {
102+
for (const child of Object.keys(nodeChildren)) {
103+
results.push(bfs(nodeChildren[child], i + 1));
104+
}
105+
} else if (nodeChildren[char]) {
106+
results.push(bfs(nodeChildren[char], i + 1));
107+
}
108+
109+
return results.some((result) => result);
110+
}
111+
112+
return bfs(this.root, 0);
113+
}
114+
}
115+
116+
module.exports = { TrieNode, WordDictionary };
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const { WordDictionary } = require("./designAddAndSearchWordsDataStructure");
2+
3+
describe("WordDictionary", () => {
4+
it("instantiates a new version of the WordDictionary class", () => {
5+
const wordDictionary = new WordDictionary();
6+
expect(wordDictionary instanceof WordDictionary).toBe(true);
7+
});
8+
9+
it("adds and can search a word to the word dictionary", () => {
10+
const wordDictionary = new WordDictionary();
11+
wordDictionary.addWord("test");
12+
expect(wordDictionary.search("test")).toBe(true);
13+
});
14+
15+
it("returns false if a word is not found in the word dictionary", () => {
16+
const wordDictionary = new WordDictionary();
17+
wordDictionary.addWord("test");
18+
expect(wordDictionary.search("tesy")).toBe(false);
19+
});
20+
21+
it("returns true if a word is found in the word dictionary using a dot", () => {
22+
const wordDictionary = new WordDictionary();
23+
wordDictionary.addWord("test");
24+
expect(wordDictionary.search("t.st")).toBe(true);
25+
});
26+
27+
it("returns true if a word is found in the word dictionary using multiple dots", () => {
28+
const wordDictionary = new WordDictionary();
29+
wordDictionary.addWord("test");
30+
expect(wordDictionary.search("t..t")).toBe(true);
31+
});
32+
33+
it("returns false if the word is not found in the word dictionary using a dot", () => {
34+
const wordDictionary = new WordDictionary();
35+
wordDictionary.addWord("test");
36+
expect(wordDictionary.search("..ss")).toBe(false);
37+
});
38+
39+
it("returns false if the word is not found in the word dictionary using multiple dots longer than word length", () => {
40+
const wordDictionary = new WordDictionary();
41+
wordDictionary.addWord("test");
42+
expect(wordDictionary.search("tes..")).toBe(false);
43+
});
44+
});

0 commit comments

Comments
 (0)