-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path235.js
More file actions
31 lines (26 loc) · 686 Bytes
/
235.js
File metadata and controls
31 lines (26 loc) · 686 Bytes
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
/**
* @param {string} pattern
* @param {string} s
* @return {boolean}
*/
var wordPattern = function (pattern, s) {
const bow = new Map(); // word -> char
const boc = new Map(); // char -> word
const chunks = s.split(" ");
if (chunks.length !== pattern.length) {
return false;
}
for (let i = 0; i < chunks.length; i++) {
const ch = pattern[i];
const chunk = chunks[i];
const digit = bow.has(chunk) ? bow.get(chunk) : "";
const word = boc.has(ch) ? boc.get(ch) : "";
if (word + digit === "") {
bow.set(chunk, ch);
boc.set(ch, chunk);
} else if (word !== chunk || digit !== ch) {
return false;
}
}
return true;
};