-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordPatternII.js
More file actions
80 lines (69 loc) · 1.96 KB
/
Copy pathwordPatternII.js
File metadata and controls
80 lines (69 loc) · 1.96 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Given a pattern and a string str, find if str follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty substring in str.
Examples:
pattern = "abab", str = "redblueredblue" should return true.
pattern = "aaaa", str = "asdasdasdasd" should return true.
pattern = "aabb", str = "xyzabcxzyabc" should return false.
Notes:
You may assume both pattern and str contains only lowercase letters.
* @param {string} pattern
* @param {string} str
* @return {boolean}
*/
const Match = (
pattern,
str,
indexOfPattern,
indexOfStr,
mapOfCharacters,
taken,
) => {
if (pattern.length === indexOfPattern && str.length === indexOfStr)
return true;
const characterInPattern = pattern[indexOfPattern];
if (mapOfCharacters.has(characterInPattern)) {
// if in the map
const patternMappedToStr = mapOfCharacters.get(characterInPattern);
if (!str.startsWith(patternMappedToStr, indexOfStr)) {
return false;
}
if (indexOfPattern < pattern.length) {
return Match(
pattern,
str,
++indexOfPattern,
indexOfStr + patternMappedToStr.length,
mapOfCharacters,
taken,
);
} else {
return true;
}
} else {
// if not in the map
let candidate = '';
for (let i = indexOfStr; i < str.length; i += 1) {
candidate += str[i];
if (taken.has(candidate)) {
continue;
}
mapOfCharacters.set(characterInPattern, candidate);
taken.add(candidate);
if (
Match(pattern, str, ++indexOfPattern, i + 1, mapOfCharacters, taken)
) {
return true;
}
--indexOfPattern;
taken.delete(candidate);
mapOfCharacters.delete(characterInPattern);
}
}
return false;
};
var wordPatternMatch = function(pattern, str) {
const mapOfCharacters = new Map();
const taken = new Set();
return Match(pattern, str, 0, 0, mapOfCharacters, taken);
};