-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0676-implement-magic-dictionary.js
45 lines (42 loc) · 1.36 KB
/
0676-implement-magic-dictionary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* 676. Implement Magic Dictionary
* https://leetcode.com/problems/implement-magic-dictionary/
* Difficulty: Medium
*
* Design a data structure that is initialized with a list of different words. Provided a string,
* you should determine if you can change exactly one character in this string to match any word
* in the data structure.
*
* Implement the MagicDictionary class:
* - MagicDictionary() Initializes the object.
* - void buildDict(String[] dictionary) Sets the data structure with an array of distinct
* strings dictionary.
* - bool search(String searchWord) Returns true if you can change exactly one character in
* searchWord to match any string in the data structure, otherwise returns false.
*/
var MagicDictionary = function() {
this.words = new Set();
};
/**
* @param {string[]} dictionary
* @return {void}
*/
MagicDictionary.prototype.buildDict = function(dictionary) {
this.words = new Set(dictionary);
};
/**
* @param {string} searchWord
* @return {boolean}
*/
MagicDictionary.prototype.search = function(searchWord) {
const words = searchWord.split('');
return Array.from(this.words).some(word => {
if (word.length !== searchWord.length) return false;
let diff = 0;
for (let i = 0; i < word.length; i++) {
if (word[i] !== words[i]) diff++;
if (diff > 1) return false;
}
return diff === 1;
});
};