Skip to content

Commit 8ff315b

Browse files
authored
Create 3298-count-substrings-that-can-be-rearranged-to-contain-a-string-ii.js
1 parent 1dfef76 commit 8ff315b

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @param {string} word1
3+
* @param {string} word2
4+
* @return {number}
5+
*/
6+
var validSubstringCount = function (word1, word2) {
7+
const mp1 = new Array(26).fill(0)
8+
const mp2 = new Array(26).fill(0)
9+
let count = 0
10+
11+
for (const c of word2) {
12+
mp1[c.charCodeAt(0) - 'a'.charCodeAt(0)]++
13+
}
14+
15+
for (let i = 0; i < 26; i++) {
16+
if (mp1[i] > 0) {
17+
count += mp1[i]
18+
}
19+
}
20+
21+
let res = 0
22+
let left = 0
23+
24+
for (let right = 0; right < word1.length; right++) {
25+
mp2[word1.charCodeAt(right) - 'a'.charCodeAt(0)]++
26+
if (
27+
mp1[word1.charCodeAt(right) - 'a'.charCodeAt(0)] > 0 &&
28+
mp2[word1.charCodeAt(right) - 'a'.charCodeAt(0)] <=
29+
mp1[word1.charCodeAt(right) - 'a'.charCodeAt(0)]
30+
) {
31+
count--
32+
}
33+
34+
while (count === 0) {
35+
res += word1.length - right
36+
if (
37+
mp1[word1.charCodeAt(left) - 'a'.charCodeAt(0)] > 0 &&
38+
mp2[word1.charCodeAt(left) - 'a'.charCodeAt(0)] <=
39+
mp1[word1.charCodeAt(left) - 'a'.charCodeAt(0)]
40+
) {
41+
count++
42+
}
43+
mp2[word1.charCodeAt(left) - 'a'.charCodeAt(0)]--
44+
left++
45+
}
46+
}
47+
48+
return res
49+
}

0 commit comments

Comments
 (0)