Count the Number of Vowel Strings in Range

Easy
637.

Count the Number of Vowel Strings in Range

Count the Number of Vowel Strings in Range is an easy coding problem on arrays and strings, asked in TCS, Wipro and Cognizant interviews.

You are given a 0-indexed array of strings words and two integers left and right.

A string is a vowel string if it both starts and ends with a vowel (a, e, i, o or u). Return how many indices i with left <= i <= right hold a vowel string.

Example 1

Input
words = ["are","amy","u"], left = 0, right = 2
Output
2
Explanation
"are" starts with a and ends with e; "u" is a single vowel, which counts for both ends; "amy" ends in y.

Example 2

Input
words = ["hey","aeo","mu","ooo","artro"], left = 1, right = 4
Output
3
Explanation
"aeo", "ooo" and "artro" qualify.

Example 3

Input
words = ["kata","echo"], left = 1, right = 1
Output
1

Constraints

  • 1 <= words.length <= 1000
  • 1 <= words[i].length <= 10
  • 0 <= left <= right < words.length
  • words[i] consists of lowercase English letters.
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string[]} words
 * @param {number} left
 * @param {number} right
 * @return {number}
 */
var vowelStrings = function(words, left, right) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Count the Number of Vowel Strings in Range — Solution

Count the Number of Vowel Strings in Range solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript.

Count the Number of Vowel Strings in Range is an easy coding problem on arrays and strings, asked in TCS, Wipro and Cognizant interviews.

  • Difficulty: Easy
  • Topics: Arrays, Strings
  • Asked at: TCS, Wipro, Cognizant
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

You are given a 0-indexed array of strings words and two integers left and right.

A string is a vowel string if it both starts and ends with a vowel (a, e, i, o or u). Return how many indices i with left <= i <= right hold a vowel string.

Example 1

Input: words = ["are","amy","u"], left = 0, right = 2
Output: 2
Explanation: "are" starts with a and ends with e; "u" is a single vowel, which counts for both ends; "amy" ends in y.

Example 2

Input: words = ["hey","aeo","mu","ooo","artro"], left = 1, right = 4
Output: 3
Explanation: "aeo", "ooo" and "artro" qualify.

Example 3

Input: words = ["kata","echo"], left = 1, right = 1
Output: 1

Constraints

  • 1 <= words.length <= 1000
  • 1 <= words[i].length <= 10
  • 0 <= left <= right < words.length
  • words[i] consists of lowercase English letters.

Hints for Count the Number of Vowel Strings in Range

  1. Hint 1

    Only the first and last character of each word matter — the middle is irrelevant.

  2. Hint 2

    A one-character word is its own first and last character.

  3. Hint 3

    Loop the index range directly rather than filtering the whole array.

How to solve Count the Number of Vowel Strings in Range

The predicate touches two characters per word, so the whole problem is a bounded loop with a two-character test.

Approach

  1. Loop i from left to right inclusive.
  2. Take w = words[i] and test whether w[0] and w[w.length - 1] are both vowels.
  3. Count the words that pass.

Why it works

A word of length 1 has w[0] == w[w.length - 1], so the same test correctly requires that single character to be a vowel — no special case needed.

Complexity

  • Time — O(right - left)
  • Space — O(1)

Pitfalls

  • right is inclusive; i < right drops the last word.
  • Treating 'y' as a vowel — the statement lists exactly five.

Reference solution

Python

from typing import List

def vowelStrings(words: List[str], left: int, right: int) -> int:
    vowels = set("aeiou")
    total = 0
    for i in range(left, right + 1):
        w = words[i]
        if w[0] in vowels and w[-1] in vowels:
            total += 1
    return total

Java

public static int vowelStrings(String[] words, int left, int right) {
    String v = "aeiou";
    int total = 0;
    for (int i = left; i <= right; i++) {
        String w = words[i];
        if (v.indexOf(w.charAt(0)) >= 0 && v.indexOf(w.charAt(w.length() - 1)) >= 0) total++;
    }
    return total;
}

C++

int vowelStrings(vector<string>& words, int left, int right) {
    string v = "aeiou";
    int total = 0;
    for (int i = left; i <= right; i++) {
        const string& w = words[i];
        if (v.find(w.front()) != string::npos && v.find(w.back()) != string::npos) total++;
    }
    return total;
}

JavaScript

var vowelStrings = function(words, left, right) {
    var total = 0;
    for (var i = left; i <= right; i++) {
        var w = words[i];
        if ("aeiou".indexOf(w.charAt(0)) >= 0 && "aeiou".indexOf(w.charAt(w.length - 1)) >= 0) total++;
    }
    return total;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 1199 arrays problems · the whole catalogue

Learn the technique: Arrays · Strings