Skip to content

Commit e9909d1

Browse files
authored
Update 2301-match-substring-after-replacement.js
1 parent 0367f05 commit e9909d1

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

2301-match-substring-after-replacement.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,44 @@
1+
/**
2+
* @param {string} ch
3+
*/
4+
function encode(ch) {
5+
let c = ch.charCodeAt(0)
6+
const a = 'a'.charCodeAt(0)
7+
const A = 'A'.charCodeAt(0)
8+
const zero = '0'.charCodeAt(0)
9+
if (c >= a) return c - a;
10+
if (c >= A) return c - A + 26;
11+
return c - zero + 52;
12+
}
13+
14+
/**
15+
* @param {string} s
16+
* @param {string} sub
17+
* @param {character[][]} mappings
18+
* @return {boolean}
19+
*/
20+
const matchReplacement = function (s, sub, mappings) {
21+
const adj = Array(62 * 62).fill(0)
22+
const m = s.length, n = sub.length
23+
for(const ch of sub) adj[encode(ch) * 62 + encode(ch)] = 1
24+
for(const [f, t] of mappings) {
25+
adj[encode(f) * 62 + encode(t)] = 1
26+
}
27+
28+
for(let i = m - n; i >= 0; i--) {
29+
for(let si = i, j = 0;; si++, j++) {
30+
if(j === n) return true
31+
if(adj[encode(sub[j]) * 62 + encode(s[si])] === 0) break
32+
}
33+
}
34+
35+
return false
36+
};
37+
38+
39+
// another
40+
41+
142
let adj = new Uint8Array(3844);
243

344
/**

0 commit comments

Comments
 (0)