Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2062.html

2062. Count Vowel Substrings of a String (Easy)

A substring is a contiguous (non-empty) sequence of characters within a string.

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.

Given a string word, return the number of vowel substrings in word.

 

Example 1:

Input: word = "aeiouu"
Output: 2
Explanation: The vowel substrings of word are as follows (underlined):
- "aeiouu"
- "aeiouu"

Example 2:

Input: word = "unicornarihan"
Output: 0
Explanation: Not all 5 vowels are present, so there are no vowel substrings.

Example 3:

Input: word = "cuaieuouac"
Output: 7
Explanation: The vowel substrings of word are as follows (underlined):
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"
- "cuaieuouac"

Example 4:

Input: word = "bbaeixoubb"
Output: 0
Explanation: The only substrings that contain all five vowels also contain consonants, so there are no vowel substrings.

 

Constraints:

  • 1 <= word.length <= 100
  • word consists of lowercase English letters only.

Similar Questions:

Solution 1. Brute Force

  • // OJ: https://leetcode.com/problems/count-vowel-substrings-of-a-string/
    // Time: O(N^2)
    // Space: O(1)
    class Solution {
        bool isVowel(char c) {
            return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
        };
    public:
        int countVowelSubstrings(string s) {
            int ans = 0, N = s.size();
            unordered_map<char, int> cnt;
            for (int i = 0; i < N; ++i) {
                cnt.clear();
                for (int j = i; j < N && isVowel(s[j]); ++j) {
                    cnt[s[j]]++;
                    if (cnt.size() == 5) ++ans;
                }
            }
            return ans;
        }
    };
    
  • class Solution:
        def countVowelSubstrings(self, word: str) -> int:
            n = len(word)
            s = set('aeiou')
            return sum(set(word[i:j]) == s for i in range(n) for j in range(i + 1, n + 1))
    
    ############
    
    # 2062. Count Vowel Substrings of a String
    # https://leetcode.com/problems/count-vowel-substrings-of-a-string/
    
    class Solution:
        def countVowelSubstrings(self, word: str) -> int:
            res = 0
            
            n = len(word)
            
            for i in range(n):
                A = [0, 0, 0, 0, 0]
                for j in range(i, n):
                    if word[j] == 'a':
                        A[0] += 1
                    elif word[j] == 'e':
                        A[1] += 1
                    elif word[j] == 'i':
                        A[2] += 1
                    elif word[j] == 'o':
                        A[3] += 1
                    elif word[j] == 'u':
                        A[4] += 1
                    else:
                        break
                    
                    if all(x > 0 for x in A):
                        res += 1
    
            return res
    
    
  • class Solution {
        public int countVowelSubstrings(String word) {
            int n = word.length();
            int ans = 0;
            for (int i = 0; i < n; ++i) {
                Set<Character> t = new HashSet<>();
                for (int j = i; j < n; ++j) {
                    char c = word.charAt(j);
                    if (!isVowel(c)) {
                        break;
                    }
                    t.add(c);
                    if (t.size() == 5) {
                        ++ans;
                    }
                }
            }
            return ans;
        }
    
        private boolean isVowel(char c) {
            return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
        }
    }
    
  • func countVowelSubstrings(word string) int {
    	ans, n := 0, len(word)
    	for i := range word {
    		t := map[byte]bool{}
    		for j := i; j < n; j++ {
    			c := word[j]
    			if !(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
    				break
    			}
    			t[c] = true
    			if len(t) == 5 {
    				ans++
    			}
    		}
    	}
    	return ans
    }
    
  • function countVowelSubstrings(word: string): number {
        let ans = 0;
        const n = word.length;
        for (let i = 0; i < n; ++i) {
            const t = new Set<string>();
            for (let j = i; j < n; ++j) {
                const c = word[j];
                if (
                    !(c === 'a' || c === 'e' || c === 'i' || c === 'o' || c === 'u')
                ) {
                    break;
                }
                t.add(c);
                if (t.size === 5) {
                    ans++;
                }
            }
        }
        return ans;
    }
    
    

Solution 2. Sliding Window

Check out “C++ Maximum Sliding Window Cheatsheet Template!

Function atMost(s, goal) returns the number of substrings that has at most goal number of unique vowels. The answer is atMost(s, 5) - atMost(s, 4).

  • // OJ: https://leetcode.com/problems/count-vowel-substrings-of-a-string/
    // Time: O(N)
    // Space: O(1)
    class Solution {
        bool isVowel(char c) {
            return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
        };
        int atMost(string &s, int goal) {
            int ans = 0, i = 0, j = 0, N = s.size();
            unordered_map<char, int> cnt;
            for (; j < N; ++j) {
                if (!isVowel(s[j])) {
                    i = j + 1;
                    cnt.clear();
                    continue;
                }
                cnt[s[j]]++;
                for (; cnt.size() > goal; ++i) {
                    if (--cnt[s[i]] == 0) cnt.erase(s[i]);
                }
                ans += j - i + 1; // this window [i, j] is the maximum window ending at `s[j]` that has at most `goal` number of unique vowels.
            }
            return ans;
        }
    public:
        int countVowelSubstrings(string s) {
            return atMost(s, 5) - atMost(s, 4);
        }
    };
    
  • class Solution:
        def countVowelSubstrings(self, word: str) -> int:
            n = len(word)
            s = set('aeiou')
            return sum(set(word[i:j]) == s for i in range(n) for j in range(i + 1, n + 1))
    
    ############
    
    # 2062. Count Vowel Substrings of a String
    # https://leetcode.com/problems/count-vowel-substrings-of-a-string/
    
    class Solution:
        def countVowelSubstrings(self, word: str) -> int:
            res = 0
            
            n = len(word)
            
            for i in range(n):
                A = [0, 0, 0, 0, 0]
                for j in range(i, n):
                    if word[j] == 'a':
                        A[0] += 1
                    elif word[j] == 'e':
                        A[1] += 1
                    elif word[j] == 'i':
                        A[2] += 1
                    elif word[j] == 'o':
                        A[3] += 1
                    elif word[j] == 'u':
                        A[4] += 1
                    else:
                        break
                    
                    if all(x > 0 for x in A):
                        res += 1
    
            return res
    
    
  • class Solution {
        public int countVowelSubstrings(String word) {
            int n = word.length();
            int ans = 0;
            for (int i = 0; i < n; ++i) {
                Set<Character> t = new HashSet<>();
                for (int j = i; j < n; ++j) {
                    char c = word.charAt(j);
                    if (!isVowel(c)) {
                        break;
                    }
                    t.add(c);
                    if (t.size() == 5) {
                        ++ans;
                    }
                }
            }
            return ans;
        }
    
        private boolean isVowel(char c) {
            return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
        }
    }
    
  • func countVowelSubstrings(word string) int {
    	ans, n := 0, len(word)
    	for i := range word {
    		t := map[byte]bool{}
    		for j := i; j < n; j++ {
    			c := word[j]
    			if !(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
    				break
    			}
    			t[c] = true
    			if len(t) == 5 {
    				ans++
    			}
    		}
    	}
    	return ans
    }
    
  • function countVowelSubstrings(word: string): number {
        let ans = 0;
        const n = word.length;
        for (let i = 0; i < n; ++i) {
            const t = new Set<string>();
            for (let j = i; j < n; ++j) {
                const c = word[j];
                if (
                    !(c === 'a' || c === 'e' || c === 'i' || c === 'o' || c === 'u')
                ) {
                    break;
                }
                t.add(c);
                if (t.size === 5) {
                    ans++;
                }
            }
        }
        return ans;
    }
    
    

Discuss

https://leetcode.com/problems/count-vowel-substrings-of-a-string/discuss/1563765/C%2B%2B-O(N)-Time-Sliding-Window

All Problems

All Solutions