Welcome to Subscribe On Youtube

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

2063. Vowels of All Substrings (Medium)

Given a string word, return the sum of the number of vowels ('a', 'e', 'i', 'o', and 'u') in every substring of word.

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

Note: Due to the large constraints, the answer may not fit in a signed 32-bit integer. Please be careful during the calculations.

 

Example 1:

Input: word = "aba"
Output: 6
Explanation: 
All possible substrings are: "a", "ab", "aba", "b", "ba", and "a".
- "b" has 0 vowels in it
- "a", "ab", "ba", and "a" have 1 vowel each
- "aba" has 2 vowels in it
Hence, the total sum of vowels = 0 + 1 + 1 + 1 + 1 + 2 = 6. 

Example 2:

Input: word = "abc"
Output: 3
Explanation: 
All possible substrings are: "a", "ab", "abc", "b", "bc", and "c".
- "a", "ab", and "abc" have 1 vowel each
- "b", "bc", and "c" have 0 vowels each
Hence, the total sum of vowels = 1 + 1 + 1 + 0 + 0 + 0 = 3. 

Example 3:

Input: word = "ltcd"
Output: 0
Explanation: There are no vowels in any substring of "ltcd".

Example 4:

Input: word = "noosabasboosa"
Output: 237
Explanation: There are a total of 237 vowels in all the substrings.

 

Constraints:

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

Similar Questions:

Solution 1.

If s[i] is vowel, there are (i + 1) * (N - i) substrings that contain s[i] where i + 1 and N - i are the number of possible left and right end points of the substrings, respectively.

  • // OJ: https://leetcode.com/problems/vowels-of-all-substrings/
    // Time: O(N)
    // Space: O(1)
    class Solution {
        bool isVowel(char c) {
            return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
        };
    public:
        long long countVowels(string s) {
            long long N = s.size(), ans = 0;
            for (long long i = 0; i < N; ++i) {
                if (!isVowel(s[i])) continue;
                ans += (i + 1) * (N - i);
            }
            return ans;
        }
    };
    
  • class Solution:
        def countVowels(self, word: str) -> int:
            n = len(word)
            return sum((i + 1) * (n - i) for i, c in enumerate(word) if c in 'aeiou')
    
    ############
    
    # 2063. Vowels of All Substrings
    # https://leetcode.com/problems/vowels-of-all-substrings/
    
    class Solution:
        def countVowels(self, word: str) -> int:
            n = len(word)
            res = 0
    
            for i, s in enumerate(word):
                if s == "a" or s == "e" or s == "i" or s == "o" or s == "u":
                    res += ((n - i) * (i + 1))           
    
            return res
    
    
  • class Solution {
        public long countVowels(String word) {
            long ans = 0;
            for (int i = 0, n = word.length(); i < n; ++i) {
                char c = word.charAt(i);
                if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
                    ans += (i + 1L) * (n - i);
                }
            }
            return ans;
        }
    }
    
  • func countVowels(word string) (ans int64) {
    	for i, c := range word {
    		if c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' {
    			ans += int64((i + 1) * (len(word) - i))
    		}
    	}
    	return
    }
    
  • function countVowels(word: string): number {
        const n = word.length;
        let ans = 0;
        for (let i = 0; i < n; ++i) {
            if (['a', 'e', 'i', 'o', 'u'].includes(word[i])) {
                ans += (i + 1) * (n - i);
            }
        }
        return ans;
    }
    
    

Discuss

https://leetcode.com/problems/vowels-of-all-substrings/discuss/1563756/C%2B%2B-O(N)-Time

All Problems

All Solutions