Understand Count Vowel Strings in Ranges Problem

Problem Name: Count Vowel Strings in Ranges
Problem Description:

Count Vowel Strings in Ranges

Problem Description:

You are given:

  1. An array of strings words: These are lowercase English words.
  2. A list of queries queries: Each query is a range [li, ri].

Task:
For each query [li, ri], count how many words in the range (from index li to ri, inclusive) start and end with a vowel.

What are vowels?

The vowels are 'a', 'e', 'i', 'o', and 'u'.

Output:

Return an array ans where:

  • ans[i] is the answer to the i-th query.

Example 1:

Input: words = ["aba", "bcb", "ece", "aa", "e"],           queries = [[0, 2], [1, 4], [1, 1]]
Output: [2, 3, 0]

Explanation:

Words starting and ending with vowels:
"aba", "ece", "aa", "e".

Query Results:

  • For [0, 2]:
    The range contains "aba" and "ece", so the result is 2.
  • For [1, 4]:
    The range contains "ece", "aa", "e", so the result is 3.
  • For [1, 1]:
    No word in this range satisfies the condition, so the result is 0.

Example 2:

Input: words = ["a","e","i"],                       queries = [[0,2],[0,1],[2,2]]

Output: [2, 3, 0]

Explanation:

All words start and end with vowels.

Query Results:

  • For [0, 2]:
    All words in this range ("a", "e", "i") satisfy the condition, so the result is 3.
  • For [0, 1]:
    The range contains "a" and "e", so the result is 2.
  • For [2, 2]:
    The range contains "i", so the result is 1.

Constraints:

  • Number of words (words.length): Up to 10510^5.
  • Length of a single word (words[i].length): Up to 4040.
  • Total characters in all words combined: Up to 3×1053 \times 10^5.
  • Number of queries (queries.length): Up to 10510^5.
  • Query range: Always valid, i.e., 0liri<words.length0 \leq l_i \leq r_i < \text{words.length}.
Category:
  • 2D Arrays
Programming Language:
  • Java
Reference Link:

https://leetcode.com/problems/count-vowel-strings-in-ranges/description/?envType=daily-question&envId=2025-01-02

Online IDE

Scroll down for output
Java
Output:

Loading component...

Loading component...

Tracking code (View only. In case you want to track the code, click this button):
Main Function:

Main Function is not defined.

Helper Function:

INPUT: words = ["aba", "bcb", "ece", "aa", "e"], queries = [[0, 2], [1, 4], [1, 1]]

OUTPUT : [2, 3, 0]

public static int[] vowelString( string[] words, int[][] queries){

int n = words.length;

int[] Prefix = new int[n + 1];

Set<Character> vowels = new HashSet<>(Arrays.asList('a' , 'e' , 'i' , 'o' , 'u'));

for (int i = 0; i < n ; i ++){

Prefix[ i+1 ] = Prefix[ i ];

if(vowels.contains(words[i].charAt(0)) && vowels.contains(words[i].charAt(words[i].length()-1))){

Prefix[i+1]++;

}//If End

}//Loop End

int ANS = new int[queries.length];

for(int i = 0; i < queries.length; i++){

ANS[i] = Prefix[queries[i][1] + 1] - Prefix[queries[i][0]];

}//Loop End

return ANS;

}//function end

Utility Functions and Global variables:

Utility Function is not required.