-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2947-count-beautiful-substrings-i.js
More file actions
51 lines (46 loc) · 1.29 KB
/
2947-count-beautiful-substrings-i.js
File metadata and controls
51 lines (46 loc) · 1.29 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
/**
* 2947. Count Beautiful Substrings I
* https://leetcode.com/problems/count-beautiful-substrings-i/
* Difficulty: Medium
*
* You are given a string s and a positive integer k.
*
* Let vowels and consonants be the number of vowels and consonants in a string.
*
* A string is beautiful if:
* - vowels == consonants.
* - (vowels * consonants) % k == 0, in other terms the multiplication of vowels and
* consonants is divisible by k.
*
* Return the number of non-empty beautiful substrings in the given string s.
*
* A substring is a contiguous sequence of characters in a string.
*
* Vowel letters in English are 'a', 'e', 'i', 'o', and 'u'.
*
* Consonant letters in English are every letter except vowels.
*/
/**
* @param {string} s
* @param {number} k
* @return {number}
*/
var beautifulSubstrings = function(s, k) {
const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
let result = 0;
for (let start = 0; start < s.length; start++) {
let vowelCount = 0;
let consonantCount = 0;
for (let end = start; end < s.length; end++) {
if (vowels.has(s[end])) {
vowelCount++;
} else {
consonantCount++;
}
if (vowelCount === consonantCount && (vowelCount * consonantCount) % k === 0) {
result++;
}
}
}
return result;
};