|
| 1 | +/** |
| 2 | + * [2062] Count Vowel Substrings of a String |
| 3 | + * |
| 4 | + * A substring is a contiguous (non-empty) sequence of characters within a string. |
| 5 | + * A vowel substring is a substring that only consists of vowels ('a', 'e', 'i', 'o', and 'u') and has all five vowels present in it. |
| 6 | + * Given a string word, return the number of vowel substrings in word. |
| 7 | + * |
| 8 | + * Example 1: |
| 9 | + * |
| 10 | + * Input: word = "aeiouu" |
| 11 | + * Output: 2 |
| 12 | + * Explanation: The vowel substrings of word are as follows (underlined): |
| 13 | + * - "<u>aeiou</u>u" |
| 14 | + * - "<u>aeiouu</u>" |
| 15 | + * |
| 16 | + * Example 2: |
| 17 | + * |
| 18 | + * Input: word = "unicornarihan" |
| 19 | + * Output: 0 |
| 20 | + * Explanation: Not all 5 vowels are present, so there are no vowel substrings. |
| 21 | + * |
| 22 | + * Example 3: |
| 23 | + * |
| 24 | + * Input: word = "cuaieuouac" |
| 25 | + * Output: 7 |
| 26 | + * Explanation: The vowel substrings of word are as follows (underlined): |
| 27 | + * - "c<u>uaieuo</u>uac" |
| 28 | + * - "c<u>uaieuou</u>ac" |
| 29 | + * - "c<u>uaieuoua</u>c" |
| 30 | + * - "cu<u>aieuo</u>uac" |
| 31 | + * - "cu<u>aieuou</u>ac" |
| 32 | + * - "cu<u>aieuoua</u>c" |
| 33 | + * - "cua<u>ieuoua</u>c" |
| 34 | + * |
| 35 | + * |
| 36 | + * Constraints: |
| 37 | + * |
| 38 | + * 1 <= word.length <= 100 |
| 39 | + * word consists of lowercase English letters only. |
| 40 | + * |
| 41 | + */ |
| 42 | +pub struct Solution {} |
| 43 | + |
| 44 | +// problem: https://leetcode.com/problems/count-vowel-substrings-of-a-string/ |
| 45 | +// discuss: https://leetcode.com/problems/count-vowel-substrings-of-a-string/discuss/?currentPage=1&orderBy=most_votes&query= |
| 46 | + |
| 47 | +// submission codes start here |
| 48 | + |
| 49 | +impl Solution { |
| 50 | + pub fn count_vowel_substrings(word: String) -> i32 { |
| 51 | + let mut count = 0; |
| 52 | + let s = word.as_bytes(); |
| 53 | + |
| 54 | + for i in 0..s.len() { |
| 55 | + let mut st = std::collections::HashSet::new(); |
| 56 | + for j in i..s.len() { |
| 57 | + if !"aeiou".contains(s[j] as char) { |
| 58 | + break; |
| 59 | + } else { |
| 60 | + st.insert(s[j]); |
| 61 | + if st.len() == 5 { |
| 62 | + count += 1; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + count |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// submission codes end |
| 73 | + |
| 74 | +#[cfg(test)] |
| 75 | +mod tests { |
| 76 | + use super::*; |
| 77 | + |
| 78 | + #[test] |
| 79 | + fn test_2062_example_1() { |
| 80 | + let word = "aeiouu".to_string(); |
| 81 | + |
| 82 | + let result = 2; |
| 83 | + |
| 84 | + assert_eq!(Solution::count_vowel_substrings(word), result); |
| 85 | + } |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn test_2062_example_2() { |
| 89 | + let word = "unicornarihan".to_string(); |
| 90 | + |
| 91 | + let result = 0; |
| 92 | + |
| 93 | + assert_eq!(Solution::count_vowel_substrings(word), result); |
| 94 | + } |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn test_2062_example_3() { |
| 98 | + let word = "cuaieuouac".to_string(); |
| 99 | + |
| 100 | + let result = 7; |
| 101 | + |
| 102 | + assert_eq!(Solution::count_vowel_substrings(word), result); |
| 103 | + } |
| 104 | +} |
0 commit comments