Skip to content

Latest commit

 

History

History
45 lines (29 loc) · 697 Bytes

File metadata and controls

45 lines (29 loc) · 697 Bytes

Count Vowel Substrings

Problem Link

https://leetcode.com/problems/count-vowel-substrings/


Pattern

  • Sliding Window

Approach

For each position, expand window to include all vowels; count substrings ending at each vowel.


Time Complexity

O(n)

Space Complexity

O(1)


Java Solution

class Solution {
    public int countVowelSubstrings(String word) {
        int ans = 0, left = 0;
        String vowels = "aeiou";
        for (int right = 0; right < word.length(); right++) {
            if (!vowels.contains(String.valueOf(word.charAt(right)))) left = right + 1;
            else ans += right - left + 1;
        }
        return ans;
    }
}