The Count Beautiful Substrings I interview question asks you to count the number of non-empty substrings in a given string s that are "beautiful." A substring is beautiful if:
k.Amazon uses this String interview pattern to test your ability to handle multiple constraints and optimize substring searches. Since this is version "I" of the problem, the constraints are usually small enough for an O(N^2) solution. It evaluates your basic string manipulation and modular arithmetic knowledge.
The pattern is Brute Force with Enumeration.
i.j.s[i..j], count the vowels and consonants.vowels == consonants AND (vowels * consonants) % k == 0.s = "baeyh", k = 2
Substrings:
"ae": Vowels=2, Consonants=0. No."ba": Vowels=1, Consonants=1. 1*1 = 1. 1 % 2 != 0. No."baey": Vowels=2, Consonants=2. 2*2 = 4. 4 % 2 == 0. Yes!
Result: 1.Even if the O(N^2) solution passes, always mention how you could optimize it using a Hash Map and Prefix Sums. This leads directly into the "Hard" version of this problem.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Count Beautiful Substrings II | Hard | Solve | |
| Number of Substrings With Fixed Ratio | Medium | Solve | |
| Sum of Largest Prime Substrings | Medium | Solve | |
| Fraction to Recurring Decimal | Medium | Solve | |
| Integer to Roman | Medium | Solve |