{"id":9929,"date":"2023-02-04T21:25:04","date_gmt":"2023-02-05T05:25:04","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9929"},"modified":"2023-02-04T21:25:38","modified_gmt":"2023-02-05T05:25:38","slug":"leetcode-2559-count-vowel-strings-in-ranges","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-2559-count-vowel-strings-in-ranges\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2559.\u00a0Count Vowel Strings in Ranges"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;array of strings&nbsp;<code>words<\/code>&nbsp;and a 2D array of integers&nbsp;<code>queries<\/code>.<\/p>\n\n\n\n<p>Each query&nbsp;<code>queries[i] = [l<sub>i<\/sub>, r<sub>i<\/sub>]<\/code>&nbsp;asks us to find the number of strings present in the range&nbsp;<code>l<sub>i<\/sub><\/code>&nbsp;to&nbsp;<code>r<sub>i<\/sub><\/code>&nbsp;(both&nbsp;<strong>inclusive<\/strong>) of&nbsp;<code>words<\/code>&nbsp;that start and end with a vowel.<\/p>\n\n\n\n<p>Return an array&nbsp;<code>ans<\/code>&nbsp;of size&nbsp;<code>queries.length<\/code>, where&nbsp;<code>ans[i]<\/code>&nbsp;is the answer to the&nbsp;<code>i<\/code><sup>th<\/sup>&nbsp;query.<\/p>\n\n\n\n<p><strong>Note<\/strong>&nbsp;that the vowel letters are&nbsp;<code>'a'<\/code>,&nbsp;<code>'e'<\/code>,&nbsp;<code>'i'<\/code>,&nbsp;<code>'o'<\/code>, and&nbsp;<code>'u'<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> words = [\"aba\",\"bcb\",\"ece\",\"aa\",\"e\"], queries = [[0,2],[1,4],[1,1]]\n<strong>Output:<\/strong> [2,3,0]\n<strong>Explanation:<\/strong> The strings starting and ending with a vowel are \"aba\", \"ece\", \"aa\" and \"e\".\nThe answer to the query [0,2] is 2 (strings \"aba\" and \"ece\").\nto query [1,4] is 3 (strings \"ece\", \"aa\", \"e\").\nto query [1,1] is 0.\nWe return [2,3,0].\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> words = [\"a\",\"e\",\"i\"], queries = [[0,2],[0,1],[2,2]]\n<strong>Output:<\/strong> [3,2,1]\n<strong>Explanation:<\/strong> Every string satisfies the conditions, so we return [3,2,1].<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= words.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= words[i].length &lt;= 40<\/code><\/li><li><code>words[i]<\/code>&nbsp;consists only of lowercase English letters.<\/li><li><code>sum(words[i].length) &lt;= 3 * 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= queries.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= l<sub>i<\/sub> &lt;= r<sub>i<\/sub> &lt;&nbsp;words.length<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Prefix Sum<\/strong><\/h2>\n\n\n\n<p>Let sum[i] := number of valid strings in words[0:i]<\/p>\n\n\n\n<p>For each query [l, r], answer will be sum[r + 1] &#8211; sum[l]<\/p>\n\n\n\n<p>Time complexity: O(n + q)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> vowelStrings(vector<string>& words, vector<vector<int>>& queries) {\n    const size_t n = words.size();\n    vector<int> sum(n + 1);\n    auto check = [](char c) {\n      return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';\n    };\n    for (size_t i = 0; i < n; ++i)\n      sum[i + 1] = sum[i] + \n        (check(words[i][0]) &#038;&#038; check(words[i][words[i].size() - 1]));\n    vector<int> ans;\n    for (const auto& q : queries)\n      ans.push_back(sum[q[1] + 1] - sum[q[0]]);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;array of strings&nbsp;words&nbsp;and a 2D array of integers&nbsp;queries. Each query&nbsp;queries[i] = [li, ri]&nbsp;asks us to find the number of strings present in&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,200,4,449],"class_list":["post-9929","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-prefix-sum","tag-string","tag-vowel","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9929","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9929"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9929\/revisions"}],"predecessor-version":[{"id":9931,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9929\/revisions\/9931"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}