File tree 2 files changed +46
-0
lines changed
2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change 509
509
673|[ Number of Longest Increasing Subsequence] ( ./0673-number-of-longest-increasing-subsequence.js ) |Medium|
510
510
674|[ Longest Continuous Increasing Subsequence] ( ./0674-longest-continuous-increasing-subsequence.js ) |Easy|
511
511
675|[ Cut Off Trees for Golf Event] ( ./0675-cut-off-trees-for-golf-event.js ) |Hard|
512
+ 676|[ Implement Magic Dictionary] ( ./0676-implement-magic-dictionary.js ) |Medium|
512
513
680|[ Valid Palindrome II] ( ./0680-valid-palindrome-ii.js ) |Easy|
513
514
684|[ Redundant Connection] ( ./0684-redundant-connection.js ) |Medium|
514
515
686|[ Repeated String Match] ( ./0686-repeated-string-match.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 676. Implement Magic Dictionary
3
+ * https://leetcode.com/problems/implement-magic-dictionary/
4
+ * Difficulty: Medium
5
+ *
6
+ * Design a data structure that is initialized with a list of different words. Provided a string,
7
+ * you should determine if you can change exactly one character in this string to match any word
8
+ * in the data structure.
9
+ *
10
+ * Implement the MagicDictionary class:
11
+ * - MagicDictionary() Initializes the object.
12
+ * - void buildDict(String[] dictionary) Sets the data structure with an array of distinct
13
+ * strings dictionary.
14
+ * - bool search(String searchWord) Returns true if you can change exactly one character in
15
+ * searchWord to match any string in the data structure, otherwise returns false.
16
+ */
17
+
18
+ var MagicDictionary = function ( ) {
19
+ this . words = new Set ( ) ;
20
+ } ;
21
+
22
+ /**
23
+ * @param {string[] } dictionary
24
+ * @return {void }
25
+ */
26
+ MagicDictionary . prototype . buildDict = function ( dictionary ) {
27
+ this . words = new Set ( dictionary ) ;
28
+ } ;
29
+
30
+ /**
31
+ * @param {string } searchWord
32
+ * @return {boolean }
33
+ */
34
+ MagicDictionary . prototype . search = function ( searchWord ) {
35
+ const words = searchWord . split ( '' ) ;
36
+ return Array . from ( this . words ) . some ( word => {
37
+ if ( word . length !== searchWord . length ) return false ;
38
+ let diff = 0 ;
39
+ for ( let i = 0 ; i < word . length ; i ++ ) {
40
+ if ( word [ i ] !== words [ i ] ) diff ++ ;
41
+ if ( diff > 1 ) return false ;
42
+ }
43
+ return diff === 1 ;
44
+ } ) ;
45
+ } ;
You can’t perform that action at this time.
0 commit comments