LeetCode Problem

How to Solve Count Beautiful Substrings I

To solve this problem, iterate through all substrings of the given string while tracking the count of vowels and consonants. For each substring, check if the product of the vowels and consonants modulo k equals zero. This will determine if the substring is beautiful. Efficient implementation is key for larger inputs.

GhostInterview Help

Need help with Count Beautiful Substrings I without spending extra time grinding it?

GhostInterview can read Count Beautiful Substrings I from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #2947Hash Table plus MathReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Hash Table plus Math
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve this problem, iterate through all substrings of the given string while tracking the count of vowels and consonants. For each substring, check if the product of the vowels and consonants modulo k equals zero. This will determine if the substring is beautiful. Efficient implementation is key for larger inputs.

Problem Statement

You are given a string s and a positive integer k. A string is considered beautiful if the product of the number of vowels and consonants in that substring modulo k equals zero.

Your task is to count how many substrings of s are beautiful. A substring is a contiguous sequence of characters within the string. For each substring, if the condition of vowel * consonant % k == 0 holds, it is deemed beautiful.

Examples

Example 1

Input: s = "baeyh", k = 2

Output: 2

There are 2 beautiful substrings in the given string.

  • Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["y","h"]). You can see that string "aeyh" is beautiful as vowels == consonants and vowels * consonants % k == 0.
  • Substring "baeyh", vowels = 2 (["a",e"]), consonants = 2 (["b","y"]). You can see that string "baey" is beautiful as vowels == consonants and vowels * consonants % k == 0. It can be shown that there are only 2 beautiful substrings in the given string.

Example 2

Input: s = "abba", k = 1

Output: 3

There are 3 beautiful substrings in the given string.

  • Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
  • Substring "abba", vowels = 1 (["a"]), consonants = 1 (["b"]).
  • Substring "abba", vowels = 2 (["a","a"]), consonants = 2 (["b","b"]). It can be shown that there are only 3 beautiful substrings in the given string.

Example 3

Input: s = "bcdf", k = 1

Output: 0

There are no beautiful substrings in the given string.

Constraints

  • 1 <= s.length <= 1000
  • 1 <= k <= 1000
  • s consists of only English lowercase letters.

Solution Approach

Iterate Through All Substrings

First, iterate over all possible substrings of the given string s. For each substring, calculate the number of vowels and consonants.

Track Frequency with a Hash Table

Use a hash table to keep track of the frequency of vowels and consonants as you move through the string. This enables efficient recalculation of the counts without needing to recompute them from scratch for every substring.

Check Beautiful Substring Condition

For each substring, check if the product of the number of vowels and consonants is divisible by k. If the condition holds, count the substring as beautiful.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity of the solution depends on the number of substrings, which is O(n^2) for a string of length n. The space complexity is primarily determined by the hash table used to track vowel and consonant counts, which has a space complexity of O(n). Optimizations can reduce this to improve performance for large inputs.

What Interviewers Usually Probe

  • Does the candidate show an understanding of hash table use in optimization?
  • Is the candidate capable of correctly identifying when a substring satisfies the condition?
  • How well does the candidate handle the substring enumeration efficiently?

Common Pitfalls or Variants

Common pitfalls

  • Overcalculating vowel and consonant counts for every substring from scratch, which leads to a higher time complexity.
  • Failing to account for edge cases, such as when there are no vowels or consonants.
  • Incorrectly interpreting the problem's condition, leading to counting invalid substrings.

Follow-up variants

  • Modify the problem by changing the condition to check for a different mathematical property, such as the sum of vowels and consonants modulo k.
  • Extend the problem to count substrings that are beautiful and also palindromes.
  • Change the problem to find the longest beautiful substring instead of counting all beautiful substrings.

How GhostInterview Helps

  • GhostInterview helps break down the problem into clear steps, starting with efficient enumeration of substrings and use of hash tables.
  • With GhostInterview's guidance, you can avoid common pitfalls and focus on optimizing your solution for large inputs.
  • GhostInterview offers hints for checking the mathematical condition and ensures you're on the right track towards solving the problem.

Topic Pages

FAQ

What is the definition of a beautiful substring in the Count Beautiful Substrings I problem?

A beautiful substring is a substring where the product of the number of vowels and consonants modulo k equals zero.

How can I optimize the solution for Count Beautiful Substrings I?

Use a hash table to track vowel and consonant frequencies to avoid redundant recalculations, making the algorithm more efficient.

What pattern is used in the Count Beautiful Substrings I problem?

The primary pattern involves using hash tables and math to efficiently count substrings that satisfy the condition of the product of vowels and consonants modulo k.

What is the time complexity of the solution to Count Beautiful Substrings I?

The time complexity is O(n^2) due to the enumeration of substrings, where n is the length of the string.

Are there any edge cases I should consider when solving Count Beautiful Substrings I?

Yes, consider edge cases where the string contains no vowels or no consonants, and ensure to handle small strings properly.

GhostInterview Solver

Need direct help with Count Beautiful Substrings I instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Count Beautiful Substrings I from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.