Skip to content

Commit 9c1303d

Browse files
authored
Create 2949-count-beautiful-substrings-ii.js
1 parent e69dcb5 commit 9c1303d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

2949-count-beautiful-substrings-ii.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
var beautifulSubstrings = function(s, k) {
7+
let ans = 0, cur = 0;
8+
let mp = new Map();
9+
mp.set(0, [-1]);
10+
for (let i = 0; i < s.length; i++) {
11+
let ch = s[i];
12+
if (ch === 'a' || ch === 'e' || ch === 'i' || ch === 'o' || ch === 'u') {
13+
cur++;
14+
} else {
15+
cur--;
16+
}
17+
for (let x of (mp.get(cur) || []) ) {
18+
let d = (i - x) / 2;
19+
if (Math.pow(d, 2) % k === 0) {
20+
ans++;
21+
}
22+
}
23+
if (!mp.has(cur)) mp.set(cur, []);
24+
mp.get(cur).push(i);
25+
}
26+
return ans;
27+
};

0 commit comments

Comments
 (0)