2947. Count Beautiful Substrings I Medium

@problem@discussion
#Hash Table#Math#String#Enumeration#Number Theory#Prefix Sum



1/**
2 * [2947] Count Beautiful Substrings I
3 *
4 * You are given a string s and a positive integer k.
5 * Let vowels and consonants be the number of vowels and consonants in a string.
6 * A string is beautiful if:
7 * 
8 * 	vowels == consonants.
9 * 	(vowels * consonants) % k == 0, in other terms the multiplication of vowels and consonants is divisible by k.
10 * 
11 * Return the number of non-empty beautiful substrings in the given string s.
12 * A substring is a contiguous sequence of characters in a string.
13 * Vowel letters in English are 'a', 'e', 'i', 'o', and 'u'.
14 * Consonant letters in English are every letter except vowels.
15 *  
16 * <strong class="example">Example 1:
17 * 
18 * Input: s = "baeyh", k = 2
19 * Output: 2
20 * Explanation: There are 2 beautiful substrings in the given string.
21 * - Substring "b<u>aeyh</u>", vowels = 2 (["a",e"]), consonants = 2 (["y","h"]).
22 * You can see that string "aeyh" is beautiful as vowels == consonants and vowels * consonants % k == 0.
23 * - Substring "<u>baey</u>h", vowels = 2 (["a",e"]), consonants = 2 (["b","y"]). 
24 * You can see that string "baey" is beautiful as vowels == consonants and vowels * consonants % k == 0.
25 * It can be shown that there are only 2 beautiful substrings in the given string.
26 * 
27 * <strong class="example">Example 2:
28 * 
29 * Input: s = "abba", k = 1
30 * Output: 3
31 * Explanation: There are 3 beautiful substrings in the given string.
32 * - Substring "<u>ab</u>ba", vowels = 1 (["a"]), consonants = 1 (["b"]). 
33 * - Substring "ab<u>ba</u>", vowels = 1 (["a"]), consonants = 1 (["b"]).
34 * - Substring "<u>abba</u>", vowels = 2 (["a","a"]), consonants = 2 (["b","b"]).
35 * It can be shown that there are only 3 beautiful substrings in the given string.
36 * 
37 * <strong class="example">Example 3:
38 * 
39 * Input: s = "bcdf", k = 1
40 * Output: 0
41 * Explanation: There are no beautiful substrings in the given string.
42 * 
43 *  
44 * Constraints:
45 * 
46 * 	1 <= s.length <= 1000
47 * 	1 <= k <= 1000
48 * 	s consists of only English lowercase letters.
49 * 
50 */
51pub struct Solution {}
52
53// problem: https://leetcode.com/problems/count-beautiful-substrings-i/
54// discuss: https://leetcode.com/problems/count-beautiful-substrings-i/discuss/?currentPage=1&orderBy=most_votes&query=
55
56// submission codes start here
57
58impl Solution {
59    pub fn beautiful_substrings(s: String, k: i32) -> i32 {
60        0
61    }
62}
63
64// submission codes end
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn test_2947() {
72    }
73}
74


Back
© 2025 bowen.ge All Rights Reserved.