Count Beautiful Substrings II Problem
Description
LeetCode Problem 2949.
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 ofvowelsandconsonantsis divisible byk.
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.
Example 1:
Input: s = "baeyh", k = 2
Output: 2
Explanation: There are 2 beautiful substrings in the given string.
- Substring "baeyh", 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 "baeyh", 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.
Example 2:
Input: s = "abba", k = 1
Output: 3
Explanation: There are 3 beautiful substrings in the given string.
- Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
- Substring "abba", 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.
Example 3:
Input: s = "bcdf", k = 1
Output: 0
Explanation: There are no beautiful substrings in the given string.
Constraints:
1 <= s.length <= 5 * 10^41 <= k <= 1000sconsists of only English lowercase letters.
Solution
Approach: Prefix Balance + Hash Map
A beautiful substring has v vowels and v consonants, so its length is 2v, and v * v must be divisible by k. That holds exactly when v is a multiple of some fixed number m (the smallest number whose square is divisible by k, found by factoring k). So the length must be a multiple of l = 2m, which we get as the smallest l with l * l divisible by 4k.
Walk through the string keeping a running balance: add 1 for a vowel and subtract 1 for a consonant. A substring has equal vowels and consonants when the balance is the same at both ends, and its length is a multiple of l when the two end positions are congruent modulo l. So we count earlier prefixes that share the same pair (position mod l, balance) in a hash map, adding that count to the answer for each new prefix.
Complexity: O(n + sqrt(k)) time for the scan plus the factoring of k, and O(n) space.
class Solution:
def beautifulSubstrings(self, s: str, k: int) -> int:
# smallest l such that 4k divides l * l
n4 = k * 4
l = 1
i = 2
while i * i <= n4:
while n4 % (i * i) == 0:
l *= i
n4 //= i * i
if n4 % i == 0:
l *= i
n4 //= i
i += 1
if n4 > 1:
l *= n4
n = len(s)
cnt = {(l - 1, n): 1}
ans = 0
total = n
for i, c in enumerate(s):
total += 1 if c in "aeiou" else -1
key = (i % l, total)
ans += cnt.get(key, 0)
cnt[key] = cnt.get(key, 0) + 1
return ans