Input: s ="baeyh", k =2Output: 2Explanation: There are 2 beautiful substrings in the given string.- Substring "b _aeyh_ ", vowels =2(["a",e"]), consonants = 2 (["y","h"]).
You can see that string "aeyh" is beautiful as vowels == consonants and vowels * consonants % k == 0.
- Substring "_baey_ h", vowels = 2 (["a",e"]), consonants =2(["b","y"]).You can see that string "baey"is beautiful as vowels == consonants and vowels * consonants % k ==0.It can be shown that there are only 2 beautiful substrings in the given string.
Input: s ="abba", k =1Output: 3Explanation: There are 3 beautiful substrings in the given string.- Substring "_ab_ ba", vowels =1(["a"]), consonants =1(["b"]).- Substring "ab _ba_ ", vowels =1(["a"]), consonants =1(["b"]).- Substring "_abba_ ", vowels =2(["a","a"]), consonants =2(["b","b"]).It can be shown that there are only 3 beautiful substrings in the given string.
We want to count substrings where the number of vowels equals the number of consonants, and their product is divisible by k. We can use prefix sums to track the difference between vowels and consonants, and a hash map to count occurrences of each (diff, vowel count) pair. For each position, we check all previous positions with the same diff and test the product divisibility.
Use prefix sums to track the difference between vowels and consonants and the count of vowels up to each position.
Use a hash map to store for each (diff, vowel count) the list of indices where this occurs.
For each end index, for all start indices with the same diff, check if the substring’s vowel and consonant counts are equal and their product is divisible by k.
classSolution {
public:int beautifulSubstrings(string s, int k) {
unordered_set<char> vowels = {'a','e','i','o','u'};
int n = s.size(), ans =0;
vector<int> diff(n+1), vcnt(n+1);
for (int i =0; i < n; ++i) {
vcnt[i+1] = vcnt[i] + vowels.count(s[i]);
diff[i+1] = vcnt[i+1] - (i+1- vcnt[i+1]);
}
unordered_map<int, vector<int>> mp;
mp[0].push_back(0);
for (int i =1; i <= n; ++i) {
for (int j : mp[diff[i]]) {
int v = vcnt[i] - vcnt[j];
int c = (i-j) - v;
if (v == c && v * c % k ==0&& v >0) ans++;
}
mp[diff[i]].push_back(i);
}
return ans;
}
};
classSolution {
publicintbeautifulSubstrings(String s, int k) {
Set<Character> vowels = Set.of('a','e','i','o','u');
int n = s.length(), ans = 0;
int[] diff =newint[n+1], vcnt =newint[n+1];
for (int i = 0; i < n; ++i) {
vcnt[i+1]= vcnt[i]+ (vowels.contains(s.charAt(i)) ? 1 : 0);
diff[i+1]= vcnt[i+1]- (i+1 - vcnt[i+1]);
}
Map<Integer, List<Integer>> mp =new HashMap<>();
mp.computeIfAbsent(0, z->new ArrayList<>()).add(0);
for (int i = 1; i <= n; ++i) {
for (int j : mp.getOrDefault(diff[i], List.of())) {
int v = vcnt[i]- vcnt[j];
int c = (i-j) - v;
if (v == c && v * c % k == 0 && v > 0) ans++;
}
mp.computeIfAbsent(diff[i], z->new ArrayList<>()).add(i);
}
return ans;
}
}
classSolution {
funbeautifulSubstrings(s: String, k: Int): Int {
val vowels = setOf('a','e','i','o','u')
val n = s.length
val diff = IntArray(n+1)
val vcnt = IntArray(n+1)
var ans = 0for (i in0 until n) {
vcnt[i+1] = vcnt[i] + if (s[i] in vowels) 1else0 diff[i+1] = vcnt[i+1] - (i+1 - vcnt[i+1])
}
val mp = mutableMapOf(0 to mutableListOf(0))
for (i in1..n) {
for (j in mp[diff[i]] ?: emptyList()) {
val v = vcnt[i] - vcnt[j]
val c = (i-j) - v
if (v == c && v * c % k ==0&& v > 0) ans++ }
mp.getOrPut(diff[i]) { mutableListOf() }.add(i)
}
return ans
}
}
classSolution:
defbeautifulSubstrings(self, s: str, k: int) -> int:
vowels = set('aeiou')
n = len(s)
diff = [0] * (n+1)
vcnt = [0] * (n+1)
for i in range(n):
vcnt[i+1] = vcnt[i] + (s[i] in vowels)
diff[i+1] = vcnt[i+1] - ((i+1) - vcnt[i+1])
from collections import defaultdict
mp = defaultdict(list)
mp[0].append(0)
ans =0for i in range(1, n+1):
for j in mp[diff[i]]:
v = vcnt[i] - vcnt[j]
c = (i-j) - v
if v == c and v * c % k ==0and v >0:
ans +=1 mp[diff[i]].append(i)
return ans
impl Solution {
pubfnbeautiful_substrings(s: String, k: i32) -> i32 {
let vowels = [b'a', b'e', b'i', b'o', b'u'];
let n = s.len();
let s = s.as_bytes();
letmut diff =vec![0; n+1];
letmut vcnt =vec![0; n+1];
for i in0..n {
vcnt[i+1] = vcnt[i] +if vowels.contains(&s[i]) { 1 } else { 0 };
diff[i+1] = vcnt[i+1] - ((i+1) asi32- vcnt[i+1]);
}
use std::collections::HashMap;
letmut mp: HashMap<i32, Vec<usize>>= HashMap::new();
mp.entry(0).or_default().push(0);
letmut ans =0;
for i in1..=n {
iflet Some(lst) = mp.get(&diff[i]) {
for&j in lst {
let v = vcnt[i] - vcnt[j];
let c = (i-j) asi32- v;
if v == c && v * c % k ==0&& v >0 {
ans +=1;
}
}
}
mp.entry(diff[i]).or_default().push(i);
}
ans
}
}