{"id":8669,"date":"2021-11-07T00:00:45","date_gmt":"2021-11-07T07:00:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8669"},"modified":"2021-11-07T00:27:46","modified_gmt":"2021-11-07T07:27:46","slug":"leetcode-2062-count-vowel-substrings-of-a-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/sliding-window\/leetcode-2062-count-vowel-substrings-of-a-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2062. Count Vowel Substrings of a String"},"content":{"rendered":"\n<p>A&nbsp;<strong>substring<\/strong>&nbsp;is a contiguous (non-empty) sequence of characters within a string.<\/p>\n\n\n\n<p>A&nbsp;<strong>vowel substring<\/strong>&nbsp;is a substring that&nbsp;<strong>only<\/strong>&nbsp;consists of vowels (<code>'a'<\/code>,&nbsp;<code>'e'<\/code>,&nbsp;<code>'i'<\/code>,&nbsp;<code>'o'<\/code>, and&nbsp;<code>'u'<\/code>) and has&nbsp;<strong>all five<\/strong>&nbsp;vowels present in it.<\/p>\n\n\n\n<p>Given a string&nbsp;<code>word<\/code>, return&nbsp;<em>the number of&nbsp;<strong>vowel substrings<\/strong>&nbsp;in<\/em>&nbsp;<code>word<\/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> word = \"aeiouu\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The vowel substrings of word are as follows (underlined):\n- \"<strong><u>aeiou<\/u><\/strong>u\"\n- \"<strong><u>aeiouu<\/u><\/strong>\"\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> word = \"unicornarihan\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> Not all 5 vowels are present, so there are no vowel substrings.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> word = \"cuaieuouac\"\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> The vowel substrings of word are as follows (underlined):\n- \"c<strong><u>uaieuo<\/u><\/strong>uac\"\n- \"c<strong><u>uaieuou<\/u><\/strong>ac\"\n- \"c<strong><u>uaieuoua<\/u><\/strong>c\"\n- \"cu<strong><u>aieuo<\/u><\/strong>uac\"\n- \"cu<strong><u>aieuou<\/u><\/strong>ac\"\n- \"cu<strong><u>aieuoua<\/u><\/strong>c\"\n- \"cua<strong><u>ieuoua<\/u><\/strong>c\"<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> word = \"bbaeixoubb\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> The only substrings that contain all five vowels also contain consonants, so there are no vowel substrings.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= word.length &lt;= 100<\/code><\/li><li><code>word<\/code>&nbsp;consists of lowercase English letters only.<\/li><\/ul>\n\n\n\n<p>Solution 1: Brute Force<\/p>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>)<br>Space complexity: O(1)<\/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  int countVowelSubstrings(string_view word) {\n    const int n = word.size();\n    auto check = [&](string_view s) {\n      set<int> seen;\n      string vowels {\"aeiou\"};\n      for (char c : s) {\n        if (vowels.find(c) == string::npos) return false;\n        seen.insert(c);\n      }\n      return seen.size() == 5;\n    };\n    int ans = 0;\n    for (int i = 0; i < n; ++i)\n      for (int l = 5; i + l <= n; ++l)\n        if (check(word.substr(i, l))) ++ans;\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Sliding Window<\/strong> <strong>\/ Three Pointers<\/strong><\/h2>\n\n\n\n<p>Maintain a window [i, j] that contain all 5 vowels, find k s.t. [k + 1, i] no longer container 5 vowels.<br># of valid substrings end with j will be (k - i).<\/p>\n\n\n\n<p><code>##<span class=\"has-inline-color has-vivid-cyan-blue-color\">aeiou<\/span><span class=\"has-inline-color has-vivid-red-color\">aeioo<\/span>##<\/code><br><code>..i....k...j..<\/code><br>i = 3, k = 8, j = 12<\/p>\n\n\n\n<p>Valid substrings are:<br><code>aeiouaeioo<br>.eiouaeioo<br>..iouaeioo<br>...ouaeioo<br>....uaeioo<\/code><br>8 - 3 = 5<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/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  int countVowelSubstrings(string_view word) {    \n    constexpr string_view vowels{\"aeiou\"};\n    int ans = 0;\n    unordered_map<char, int> m;\n    for (char c : vowels) m[c] = 0;\n    for (size_t i = 0, j = 0, k = 0, v = 0; j < word.size(); ++j) {\n      if (m.count(word[j])) {\n        v += (++m[word[j]] == 1);\n        while (v == 5)\n          v -= (--m[word[k++]] == 0);\n        ans += k - i;\n      } else {\n        for (char c : vowels) m[c] = 0;\n        v = 0;\n        i = k = j + 1;\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A&nbsp;substring&nbsp;is a contiguous (non-empty) sequence of characters within a string. A&nbsp;vowel substring&nbsp;is a substring that&nbsp;only&nbsp;consists of vowels (&#8216;a&#8217;,&nbsp;&#8216;e&#8217;,&nbsp;&#8216;i&#8217;,&nbsp;&#8216;o&#8217;, and&nbsp;&#8216;u&#8217;) and has&nbsp;all five&nbsp;vowels present in it.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[476],"tags":[222,215,4],"class_list":["post-8669","post","type-post","status-publish","format-standard","hentry","category-sliding-window","tag-easy","tag-sliding-window","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8669","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=8669"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8669\/revisions"}],"predecessor-version":[{"id":8677,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8669\/revisions\/8677"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}