DEV Community

Prashant Mishra
Prashant Mishra

Posted on

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

Top comments (0)