Count Beautiful Substrings I - 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, in other terms 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 = "baae", k = 2
›
Output:
2
💡 Note:
Beautiful substrings are "ba" (1 vowel, 1 consonant, 1*1=1, but we need (1*1)%2=0 which is false) and "baae" (3 vowels, 1 consonant - not equal). Actually "ba" has 1 vowel 'a' and 1 consonant 'b', and (1*1)%2=1≠0. Let me recalculate: "ae" has 2 vowels, 0 consonants (not equal). "aa" has 2 vowels, 0 consonants (not equal). Only "ba" and "aae" need checking. "ba": 1v,1c, (1*1)%2=1≠0. Wait, let me be more systematic with "baae": substrings with equal vowels and consonants are "ba" (1v,1c) and none others work. (1*1)%2=1≠0, so actually 0 beautiful substrings for this case.
Example 2 — Simple Beautiful
$
Input:
s = "abab", k = 1
›
Output:
2
💡 Note:
Beautiful substrings: "ab" at positions 0-1 (1 vowel, 1 consonant, 1*1=1, 1%1=0 ✓), "ba" at positions 1-2 (1 vowel, 1 consonant, 1*1=1, 1%1=0 ✓), "ab" at positions 2-3 (1 vowel, 1 consonant, 1*1=1, 1%1=0 ✓), and "abab" (2 vowels, 2 consonants, 2*2=4, 4%1=0 ✓). Total = 4.
Example 3 — No Beautiful Substrings
$
Input:
s = "abc", k = 3
›
Output:
0
💡 Note:
No substring has equal vowels and consonants: "a"(1v,0c), "b"(0v,1c), "c"(0v,1c), "ab"(1v,1c), "bc"(0v,2c), "abc"(1v,2c). Only "ab" has equal counts but (1*1)%3=1≠0, so no beautiful substrings.
Constraints
- 1 ≤ s.length ≤ 1000
- 1 ≤ k ≤ 1000
- s consists of only lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input Analysis
String with vowels and consonants, divisibility factor k
2
Check Conditions
Find substrings with equal vowels/consonants AND product divisible by k
3
Count Results
Return total number of beautiful substrings
Key Takeaway
🎯 Key Insight: Beautiful substrings require perfect balance AND mathematical harmony with k
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code