File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 290. Word Pattern
3
+ * https://leetcode.com/problems/word-pattern/
4
+ * Difficulty: Easy
5
+ *
6
+ * Given a pattern and a string s, find if s follows the same pattern.
7
+ *
8
+ * Here follow means a full match, such that there is a bijection between
9
+ * a letter in pattern and a non-empty word in s.
10
+ */
11
+
12
+ /**
13
+ * @param {string } pattern
14
+ * @param {string } s
15
+ * @return {boolean }
16
+ */
17
+ var wordPattern = function ( pattern , s ) {
18
+ const words = s . split ( / \s + / ) ;
19
+ const map = new Map ( ) ;
20
+
21
+ return words . every ( ( word , index ) => {
22
+ const offset = pattern . length === words . length ? index + 1 : words . length / pattern . length ;
23
+ const sequence = pattern . slice ( index , offset ) ;
24
+ if ( ! map . has ( sequence ) && ! Array . from ( map . values ( ) ) . includes ( word ) ) {
25
+ map . set ( sequence , word ) ;
26
+ }
27
+ return map . get ( sequence ) === word && pattern . length <= words . length ;
28
+ } ) ;
29
+ } ;
Original file line number Diff line number Diff line change 113
113
273|[ Integer to English Words] ( ./0273-integer-to-english-words.js ) |Hard|
114
114
278|[ First Bad Version] ( ./0278-first-bad-version.js ) |Medium|
115
115
283|[ Move Zeroes] ( ./0283-move-zeroes.js ) |Easy|
116
+ 290|[ Word Pattern] ( ./0290-word-pattern.js ) |Easy|
116
117
326|[ Power of Three] ( ./0326-power-of-three.js ) |Easy|
117
118
342|[ Power of Four] ( ./0342-power-of-four.js ) |Easy|
118
119
344|[ Reverse String] ( ./0344-reverse-string.js ) |Easy|
285
286
286
287
[ MIT License] ( https://opensource.org/licenses/MIT )
287
288
288
- Copyright (c) 2019-2022 [ Josh Crozier] ( https://joshcrozier.com )
289
+ Copyright (c) 2019-2023 [ Josh Crozier] ( https://joshcrozier.com )
You can’t perform that action at this time.
0 commit comments