Count Beautiful Substrings II - Problem
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, which means the multiplication of vowels and consonants is 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.
Input & Output
Example 1 — Basic Case
$
Input:
s = "baeyh", k = 2
›
Output:
2
💡 Note:
Beautiful substrings are "baey" (V=2, C=2, 2*2=4, 4%2=0) and "aey" (V=2, C=1... wait, this doesn't work). Actually: "ae" (V=2, C=0) fails balance. Let me recalculate: "ba" (V=1, C=1, 1*1=1, 1%2≠0), "bae" (V=2, C=1) fails balance. The beautiful substrings are "baey" (V=2, C=2, 2*2%2=0) and "aeyh" (V=2, C=2, 2*2%2=0).
Example 2 — Single Character
$
Input:
s = "a", k = 1
›
Output:
0
💡 Note:
Single vowel "a" has V=1, C=0, so vowels ≠ consonants. No beautiful substrings possible.
Example 3 — Minimum Beautiful
$
Input:
s = "ab", k = 1
›
Output:
1
💡 Note:
Substring "ab" has V=1, C=1 (balanced) and 1*1=1, 1%1=0 (divisible). This is beautiful.
Constraints
- 1 ≤ s.length ≤ 5 × 104
- 1 ≤ k ≤ 1000
- s consists of only lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input Analysis
String s and divisor k
2
Balance Check
Find substrings where vowels = consonants
3
Divisibility Check
Verify (vowels × consonants) % k = 0
Key Takeaway
🎯 Key Insight: Use prefix differences to efficiently track vowel-consonant balance and apply number theory to optimize divisibility checks
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code