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

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay