Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Count Vowel Strings in Ranges

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2559" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You are given a 0-indexed array of strings words and a 2D array of integers queries. Each query queries[i] = [li, ri] asks us to find the number of strings present at the indices ranging from li to ri (both inclusive) of words that start and end with a vowel. Return an array ans of size queries.length, where ans[i] is the answer to the ith query. Note that the vowel letters are 'a', 'e', 'i', 'o', and 'u'. Example 1: Input: words = ["aba","bcb","ece","aa","e"], queries = [[0,2],[1,4],[1,1]] Output: [2,3,0] Explanation: The strings starting and ending with a vowel are "aba", "ece", "aa" and "e". The answer to the query [0,2] is 2 (strings "aba" and "ece"). to query [1,4] is 3 (strings "ece", "aa", "e"). to query [1,1] is 0. We return [2,3,0]. Example 2: Input: words = ["a","e","i"], queries = [[0,2],[0,1],[2,2]] Output: [3,2,1] Explanation: Every string satisfies the conditions, so we return [3,2,1]. Constraints: 1 <= words.length <= 105 1 <= words[i].length <= 40 words[i] consists only of lowercase English letters. sum(words[i].length) <= 3 * 105 1 <= queries.length <= 105 0 <= li <= ri < words.length

Explanation

Here's a breakdown of the solution:

  • Prefix Sum: Create a prefix sum array to efficiently calculate the number of vowel-starting and ending words within a given range.
  • Vowel Check: Define a helper function to determine if a word starts and ends with a vowel.
  • Query Processing: Iterate through the queries, using the prefix sum array to determine the count of vowel-starting and ending words within the specified range for each query.

  • Runtime Complexity: O(n + m), where n is the number of words and m is the number of queries.

  • Storage Complexity: O(n), where n is the number of words (for the prefix sum array).

Code

    def vowel_strings(words, queries):
    """
    Calculates the number of strings within given ranges that start and end with a vowel.

    Args:
        words (list of str): A list of strings.
        queries (list of list of int): A list of queries, where each query is a list [l, r] representing the range.

    Returns:
        list of int: A list of answers to the queries.
    """

    def starts_and_ends_with_vowel(word):
        """Checks if a word starts and ends with a vowel."""
        vowels = "aeiou"
        return word[0] in vowels and word[-1] in vowels

    n = len(words)
    prefix_sum = [0] * (n + 1)

    for i in range(n):
        prefix_sum[i + 1] = prefix_sum[i] + int(starts_and_ends_with_vowel(words[i]))

    ans = []
    for l, r in queries:
        ans.append(prefix_sum[r + 1] - prefix_sum[l])

    return ans

More from this blog

C

Chatmagic blog

2894 posts