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:
0
💡 Note:
For s="baae" and k=2, we check all substrings: "b"(0v,1c), "a"(1v,0c), "a"(1v,0c), "e"(1v,0c) - none have equal vowels/consonants. "ba"(1v,1c) has equal counts but (1*1)%2=1≠0. "aa"(2v,0c), "ae"(2v,0c), "baa"(2v,1c), "aae"(2v,1c), "baae"(3v,1c) - none have equal vowels/consonants. Total beautiful substrings = 0.
Example 2 — Simple Beautiful
$
Input:
s = "abab", k = 1
›
Output:
4
💡 Note:
For s="abab" and k=1, beautiful substrings are: "ab" at positions (0,1) with 1 vowel, 1 consonant, (1*1)%1=0 ✓; "ba" at positions (1,2) with 1 vowel, 1 consonant, (1*1)%1=0 ✓; "ab" at positions (2,3) with 1 vowel, 1 consonant, (1*1)%1=0 ✓; "abab" at positions (0,3) with 2 vowels, 2 consonants, (2*2)%1=0 ✓. Total = 4.
Example 3 — No Beautiful Substrings
$
Input:
s = "abc", k = 3
›
Output:
1
💡 Note:
For s="abc" and k=3, checking all substrings: "a"(1v,0c), "b"(0v,1c), "c"(0v,1c) - none equal. "ab"(1v,1c) has equal counts but (1*1)%3=1≠0. "bc"(0v,2c), "abc"(1v,2c) - not equal. Total beautiful substrings = 0.
Constraints
- 1 ≤ s.length ≤ 1000
- 1 ≤ k ≤ 1000
- s consists of only lowercase English letters
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code