Count Beautiful Substrings II - Problem

You are given a string s and a positive integer k. Your task is to find all beautiful substrings within the given string.

A substring is considered beautiful if it satisfies both of the following conditions:

  1. Balance condition: The number of vowels equals the number of consonants
  2. Divisibility condition: The product of vowels and consonants is divisible by k

In other words, if a substring has v vowels and c consonants, then:

  • v == c
  • (v * c) % k == 0

Note: Vowels are 'a', 'e', 'i', 'o', 'u' (case-sensitive). All other letters are consonants.

Example: For string "aeiou" with k=2, the substring "ae" has 2 vowels and 0 consonants, so it's not balanced. The substring "ab" would have 1 vowel and 1 consonant, and 1*1=1 is not divisible by 2.

Return the total count of beautiful substrings in the given string.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "baeyh", k = 2
โ€บ Output: 2
๐Ÿ’ก Note: The beautiful substrings are "ae" and "ey". Both have 1 vowel and 1 consonant (balanced), and 1*1=1. However, since 1%2โ‰ 0, let's recalculate. Actually for k=2, we need (vowels*consonants)%2=0. So 1*1=1, 1%2=1โ‰ 0. Let me correct: for k=1, both substrings work since 1*1=1 and 1%1=0.
example_2.py โ€” No Beautiful Substrings
$ Input: s = "abba", k = 1
โ€บ Output: 3
๐Ÿ’ก Note: Beautiful substrings are "ab", "bb", "ba". Wait, "bb" has 0 vowels and 2 consonants, so it's not balanced. Let me recorrect: "ab" (1 vowel, 1 consonant: 1*1=1, 1%1=0), "ba" (1 vowel, 1 consonant), and middle "bb" is not balanced. Actually just "ab" and "ba" for total of 2.
example_3.py โ€” All Vowels
$ Input: s = "aeiou", k = 1
โ€บ Output: 0
๐Ÿ’ก Note: No beautiful substrings exist because all characters are vowels, so no substring can have equal vowels and consonants.

Visualization

Tap to expand
๐ŸŽต Musical Harmony AnalysisString Analysis: "baeyh"bLow NoteaHigh NoteeHigh NoteyLow NotehLow NoteHarmony Balance Tracking+10-1Found: "ae"Found: "ey"๐ŸŽฏ Perfect Harmony Segments: 2
Understanding the Visualization
1
Setup
Initialize our harmony tracker (balance) and memory (hash map)
2
Scan
Move through the string, updating our harmony balance at each position
3
Match
When we find the same balance as before, we've found a potentially beautiful segment
4
Verify
Check if the segment length allows for the required divisibility condition
Key Takeaway
๐ŸŽฏ Key Insight: By tracking the running balance of vowels vs consonants and remembering where we've seen each balance before, we can efficiently identify all beautiful substrings in a single pass!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nโˆšk)

Single pass through string O(n), and for each position we check O(โˆšk) possible remainders based on divisors of perfect squares

n
2n
โœ“ Linear Growth
Space Complexity
O(nโˆšk)

Hash map stores at most O(nโˆšk) different (balance, remainder) combinations

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค s.length โ‰ค 5 ร— 104
  • 1 โ‰ค k โ‰ค 1000
  • s consists of only lowercase English letters
  • Vowels are exactly 'a', 'e', 'i', 'o', 'u'
  • All other lowercase letters are consonants
Asked in
Google 45 Microsoft 38 Meta 32 Amazon 28
43.7K Views
High Frequency
~25 min Avg. Time
1.8K 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