LeetCode 2559: Count Vowel Strings in Ranges Solution

Master LeetCode problem 2559 (Count Vowel Strings in Ranges), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2559. Count Vowel Strings in Ranges

Problem Explanation

Explanation:

To solve this problem, we iterate through each word in the given range and count the number of words that start and end with a vowel. We can achieve this by checking if the first and last character of each word is a vowel.

  • Iterate through the words within the given range.
  • Check if the first and last characters of each word are vowels.
  • Increment a counter if the conditions are met.

Time complexity: O(N * M) where N is the number of words in the range and M is the average length of the words.
Space complexity: O(1)

:

Solution Code

class Solution {
    public int[] countVowelStringsInRanges(String[] words, int[][] queries) {
        int[] ans = new int[queries.length];
        
        for (int i = 0; i < queries.length; i++) {
            int count = 0;
            int start = queries[i][0];
            int end = queries[i][1];
            
            for (int j = start; j <= end; j++) {
                if (isVowelWord(words[j])) {
                    count++;
                }
            }
            
            ans[i] = count;
        }
        
        return ans;
    }
    
    private boolean isVowelWord(String word) {
        char firstChar = word.charAt(0);
        char lastChar = word.charAt(word.length() - 1);
        
        return isVowel(firstChar) && isVowel(lastChar);
    }
    
    private boolean isVowel(char c) {
        return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2559 (Count Vowel Strings in Ranges)?

This page provides optimized solutions for LeetCode problem 2559 (Count Vowel Strings in Ranges) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2559 (Count Vowel Strings in Ranges)?

The time complexity for LeetCode 2559 (Count Vowel Strings in Ranges) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2559 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2559 in Java, C++, or Python.

Back to LeetCode Solutions