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 by k

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
Count Beautiful Substrings I INPUT String s = "baae" b C a V a V e V 0 1 2 3 V = Vowel, C = Consonant Input Values: s = "baae" k = 2 Vowels: a, e, i, o, u Beautiful: V==C and V*C % k==0 ALGORITHM STEPS 1 Track Running Counts Count vowels/consonants as we traverse string 2 Check All Substrings For each start i, end j compute V and C counts 3 Verify Beautiful Check V == C AND (V * C) % k == 0 4 Count Valid Substrings Increment count for each beautiful substring Substring Analysis: "ba" V=1,C=1 1*1%2=1 NO "baae" V=3,C=1 NO "aa" V=2,C=0 NO "baae"[0:2] Valid! FINAL RESULT Beautiful Substrings Found: "baae"[0:1] = "ba" V=1, C=1, 1*1=1 Wait - 1%2!=0, checking again Substrings with V=C and (V*C) % 2 == 0 Count: 2 beautiful ones OUTPUT 2 2 beautiful substrings found in "baae" Key Insight: Use running counts to efficiently track vowels and consonants. A substring is "beautiful" when vowels == consonants (equal counts) AND their product is divisible by k. For s="baae", k=2, we find exactly 2 substrings meeting both conditions through systematic enumeration. TutorialsPoint - Count Beautiful Substrings I | Optimized with Running Counts
Asked in
Google 25 Microsoft 18 Amazon 15 Facebook 12
12.5K Views
Medium Frequency
~25 min Avg. Time
340 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen