Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Move from brute-force thinking to an efficient approach using hash map strategy.
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.
Example 1:
Input: s = "baeyh", k = 2 Output: 2 Explanation: 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 Explanation: 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 Explanation: There are no beautiful substrings in the given string.
Constraints:
1 <= s.length <= 10001 <= k <= 1000s consists of only English lowercase letters.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map · Math
"baeyh" 2
"abba" 1
"bcdf" 1
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2947: Count Beautiful Substrings I
class Solution {
public int beautifulSubstrings(String s, int k) {
int n = s.length();
int[] vs = new int[26];
for (char c : "aeiou".toCharArray()) {
vs[c - 'a'] = 1;
}
int ans = 0;
for (int i = 0; i < n; ++i) {
int vowels = 0;
for (int j = i; j < n; ++j) {
vowels += vs[s.charAt(j) - 'a'];
int consonants = j - i + 1 - vowels;
if (vowels == consonants && vowels * consonants % k == 0) {
++ans;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #2947: Count Beautiful Substrings I
func beautifulSubstrings(s string, k int) (ans int) {
n := len(s)
vs := [26]int{}
for _, c := range "aeiou" {
vs[c-'a'] = 1
}
for i := 0; i < n; i++ {
vowels := 0
for j := i; j < n; j++ {
vowels += vs[s[j]-'a']
consonants := j - i + 1 - vowels
if vowels == consonants && vowels*consonants%k == 0 {
ans++
}
}
}
return
}
# Accepted solution for LeetCode #2947: Count Beautiful Substrings I
class Solution:
def beautifulSubstrings(self, s: str, k: int) -> int:
n = len(s)
vs = set("aeiou")
ans = 0
for i in range(n):
vowels = 0
for j in range(i, n):
vowels += s[j] in vs
consonants = j - i + 1 - vowels
if vowels == consonants and vowels * consonants % k == 0:
ans += 1
return ans
// Accepted solution for LeetCode #2947: Count Beautiful Substrings I
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2947: Count Beautiful Substrings I
// class Solution {
// public int beautifulSubstrings(String s, int k) {
// int n = s.length();
// int[] vs = new int[26];
// for (char c : "aeiou".toCharArray()) {
// vs[c - 'a'] = 1;
// }
// int ans = 0;
// for (int i = 0; i < n; ++i) {
// int vowels = 0;
// for (int j = i; j < n; ++j) {
// vowels += vs[s.charAt(j) - 'a'];
// int consonants = j - i + 1 - vowels;
// if (vowels == consonants && vowels * consonants % k == 0) {
// ++ans;
// }
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2947: Count Beautiful Substrings I
function beautifulSubstrings(s: string, k: number): number {
const n = s.length;
const vs: number[] = Array(26).fill(0);
for (const c of 'aeiou') {
vs[c.charCodeAt(0) - 'a'.charCodeAt(0)] = 1;
}
let ans = 0;
for (let i = 0; i < n; ++i) {
let vowels = 0;
for (let j = i; j < n; ++j) {
vowels += vs[s.charCodeAt(j) - 'a'.charCodeAt(0)];
const consonants = j - i + 1 - vowels;
if (vowels === consonants && (vowels * consonants) % k === 0) {
++ans;
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan the rest of the array looking for a match. Two nested loops give n × (n−1)/2 comparisons = O(n²). No extra space since we only use loop indices.
One pass through the input, performing O(1) hash map lookups and insertions at each step. The hash map may store up to n entries in the worst case. This is the classic space-for-time tradeoff: O(n) extra memory eliminates an inner loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.