|
| 1 | +/** |
| 2 | + * [2063] Vowels of All Substrings |
| 3 | + * |
| 4 | + * Given a string word, return the sum of the number of vowels ('a', 'e', 'i', 'o', and 'u') in every substring of word. |
| 5 | + * A substring is a contiguous (non-empty) sequence of characters within a string. |
| 6 | + * Note: Due to the large constraints, the answer may not fit in a signed 32-bit integer. Please be careful during the calculations. |
| 7 | + * |
| 8 | + * Example 1: |
| 9 | + * |
| 10 | + * Input: word = "aba" |
| 11 | + * Output: 6 |
| 12 | + * Explanation: |
| 13 | + * All possible substrings are: "a", "ab", "aba", "b", "ba", and "a". |
| 14 | + * - "b" has 0 vowels in it |
| 15 | + * - "a", "ab", "ba", and "a" have 1 vowel each |
| 16 | + * - "aba" has 2 vowels in it |
| 17 | + * Hence, the total sum of vowels = 0 + 1 + 1 + 1 + 1 + 2 = 6. |
| 18 | + * |
| 19 | + * Example 2: |
| 20 | + * |
| 21 | + * Input: word = "abc" |
| 22 | + * Output: 3 |
| 23 | + * Explanation: |
| 24 | + * All possible substrings are: "a", "ab", "abc", "b", "bc", and "c". |
| 25 | + * - "a", "ab", and "abc" have 1 vowel each |
| 26 | + * - "b", "bc", and "c" have 0 vowels each |
| 27 | + * Hence, the total sum of vowels = 1 + 1 + 1 + 0 + 0 + 0 = 3. |
| 28 | + * |
| 29 | + * Example 3: |
| 30 | + * |
| 31 | + * Input: word = "ltcd" |
| 32 | + * Output: 0 |
| 33 | + * Explanation: There are no vowels in any substring of "ltcd". |
| 34 | + * |
| 35 | + * |
| 36 | + * Constraints: |
| 37 | + * |
| 38 | + * 1 <= word.length <= 10^5 |
| 39 | + * word consists of lowercase English letters. |
| 40 | + * |
| 41 | + */ |
| 42 | +pub struct Solution {} |
| 43 | + |
| 44 | +// problem: https://leetcode.com/problems/vowels-of-all-substrings/ |
| 45 | +// discuss: https://leetcode.com/problems/vowels-of-all-substrings/discuss/?currentPage=1&orderBy=most_votes&query= |
| 46 | + |
| 47 | +// submission codes start here |
| 48 | + |
| 49 | +impl Solution { |
| 50 | + // Credit: https://leetcode.com/problems/vowels-of-all-substrings/solutions/5089523/functional-rust-solution/ |
| 51 | + pub fn count_vowels(word: String) -> i64 { |
| 52 | + let n = word.len() as i64; |
| 53 | + word.chars() |
| 54 | + .enumerate() |
| 55 | + .filter(|(i, c)| *c == 'a' || *c == 'e' || *c == 'i' || *c == 'o' || *c == 'u') |
| 56 | + .map(|(i, c)| i as i64) |
| 57 | + .fold(0i64, |acc, i| acc + (i + 1) * (n - i)) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// submission codes end |
| 62 | + |
| 63 | +#[cfg(test)] |
| 64 | +mod tests { |
| 65 | + use super::*; |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn test_2063_example_1() { |
| 69 | + let word = "aba".to_string(); |
| 70 | + |
| 71 | + let result = 6; |
| 72 | + |
| 73 | + assert_eq!(Solution::count_vowels(word), result); |
| 74 | + } |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn test_2063_example_2() { |
| 78 | + let word = "abc".to_string(); |
| 79 | + |
| 80 | + let result = 3; |
| 81 | + |
| 82 | + assert_eq!(Solution::count_vowels(word), result); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn test_2063_example_3() { |
| 87 | + let word = "ltcd".to_string(); |
| 88 | + |
| 89 | + let result = 0; |
| 90 | + |
| 91 | + assert_eq!(Solution::count_vowels(word), result); |
| 92 | + } |
| 93 | +} |
0 commit comments