Skip to content

Commit 0367f05

Browse files
authored
Create 2301-match-substring-after-replacement.js
1 parent 2adc62b commit 0367f05

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
let adj = new Uint8Array(3844);
2+
3+
/**
4+
* @param {string} ch
5+
*/
6+
function encode(ch) {
7+
let c = ch.charCodeAt();
8+
if (c >= 97) return c - 97;
9+
if (c >= 65) return c - 39;
10+
return c + 4;
11+
}
12+
13+
/**
14+
* @param {string} s
15+
* @param {string} sub
16+
* @param {character[][]} mappings
17+
* @return {boolean}
18+
*/
19+
var matchReplacement = function (s, sub, mappings) {
20+
let n = s.length;
21+
let m = sub.length;
22+
23+
adj.fill(0);
24+
for (let [cf, ct] of mappings) {
25+
adj[encode(cf) * 62 + encode(ct)] = 1;
26+
};
27+
for (let i = 0; i < 62; ++i) {
28+
adj[i * 62 + i] = 1;
29+
}
30+
31+
for (let l = n - m; l >= 0; --l) {
32+
for (let d = 0, r = l; ; ++d, ++r) {
33+
if (d == m) return true;
34+
if (!adj[encode(sub[d]) * 62 + encode(s[r])]) break;
35+
}
36+
}
37+
38+
return false;
39+
};

0 commit comments

Comments
 (0)