Logo

dev-resources.site

for different kinds of informations.

Count vowel strings in ranges

Published at
1/2/2025
Categories
rangequeries
leetcode
dsa
prefixsum
Author
prashantrmishra
Author
15 person written this
prashantrmishra
open
Count vowel strings in ranges

Problem

TC: O(n) for calculating the prefix[] and O(k) for getting all the counts of the vowels in the given ranges

class Solution {
    public int[] vowelStrings(String[] words, int[][] queries) {
        int result[]  = new int[queries.length];

        int prefix[] = new int[words.length];
        int currentCount =0;
        for(int i=0;i<words.length;i++){
            String  s= words[i];
            currentCount+=check(s.charAt(0)) && check(s.charAt(s.length()-1)) ? 1:0;
            prefix[i] = currentCount;
        }

        for(int i = 0;i<queries.length;i++){
            int left = queries[i][0];
            int right = queries[i][1];
            int rightCount = prefix[right];
            int leftCount = left>0 ? prefix[left-1] : 0;
            result[i]  = rightCount-leftCount;
        }
        return result;
    }
    public boolean check(char c){
        return c =='a' || c =='e' || c == 'i' || c == 'o' || c =='u';
    }
}
Enter fullscreen mode Exit fullscreen mode

Featured ones: