{"id":8399,"date":"2021-04-25T14:21:25","date_gmt":"2021-04-25T21:21:25","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8399"},"modified":"2021-04-25T14:23:19","modified_gmt":"2021-04-25T21:23:19","slug":"leetcode-1839-longest-substring-of-all-vowels-in-order","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1839-longest-substring-of-all-vowels-in-order\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1839. Longest Substring Of All Vowels in Order"},"content":{"rendered":"\n<p>A string is considered&nbsp;<strong>beautiful<\/strong>&nbsp;if it satisfies the following conditions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Each of the 5 English vowels (<code>'a'<\/code>,&nbsp;<code>'e'<\/code>,&nbsp;<code>'i'<\/code>,&nbsp;<code>'o'<\/code>,&nbsp;<code>'u'<\/code>) must appear&nbsp;<strong>at least once<\/strong>&nbsp;in it.<\/li><li>The letters must be sorted in&nbsp;<strong>alphabetical order<\/strong>&nbsp;(i.e. all&nbsp;<code>'a'<\/code>s before&nbsp;<code>'e'<\/code>s, all&nbsp;<code>'e'<\/code>s before&nbsp;<code>'i'<\/code>s, etc.).<\/li><\/ul>\n\n\n\n<p>For example, strings&nbsp;<code>\"aeiou\"<\/code>&nbsp;and&nbsp;<code>\"aaaaaaeiiiioou\"<\/code>&nbsp;are considered&nbsp;<strong>beautiful<\/strong>, but&nbsp;<code>\"uaeio\"<\/code>,&nbsp;<code>\"aeoiu\"<\/code>, and&nbsp;<code>\"aaaeeeooo\"<\/code>&nbsp;are&nbsp;<strong>not beautiful<\/strong>.<\/p>\n\n\n\n<p>Given a string&nbsp;<code>word<\/code>&nbsp;consisting of English vowels, return&nbsp;<em>the&nbsp;<strong>length of the longest beautiful substring<\/strong>&nbsp;of&nbsp;<\/em><code>word<\/code><em>. If no such substring exists, return&nbsp;<\/em><code>0<\/code>.<\/p>\n\n\n\n<p>A&nbsp;<strong>substring<\/strong>&nbsp;is a contiguous sequence of characters in a string.<\/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 = \"aeiaaioaaaaeiiiiouuuooaauuaeiu\"\n<strong>Output:<\/strong> 13\n<strong>Explanation:<\/strong> The longest beautiful substring in word is \"aaaaeiiiiouuu\" of length 13.<\/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 = \"aeeeiiiioooauuuaeiou\"\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> The longest beautiful substring in word is \"aeiou\" of length 5.\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 = \"a\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There is no beautiful substring, so return 0.\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;= 5 * 10<sup>5<\/sup><\/code><\/li><li><code>word<\/code>&nbsp;consists of characters&nbsp;<code>'a'<\/code>,&nbsp;<code>'e'<\/code>,&nbsp;<code>'i'<\/code>,&nbsp;<code>'o'<\/code>, and&nbsp;<code>'u'<\/code>.<\/li><\/ul>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Solution: Counter<\/strong><\/h1>\n\n\n\n<p>Use a counter to track how many unique vowels we saw so far. Reset the counter whenever the s[i] &lt; s[i-1]. Incase the counter if s[i] &gt; s[i &#8211; 1]. When counter becomes 5, we know we found a valid substring.<\/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 longestBeautifulSubstring(string word) {\n    const int n = word.length();\n    int ans = 0;\n    char p = 'a' - 1;\n    for (int i = 0, vowels = 0, l = 0; i < n; ++i) {\n      if (word[i] < p) {\n        vowels = (word[i] == 'a');\n        l = i;\n      } else if (word[i] > p) {\n        ++vowels;\n      }      \n      if (vowels == 5)\n        ans = max(ans, i - l + 1);\n      p = word[i];\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A string is considered&nbsp;beautiful&nbsp;if it satisfies the following conditions: Each of the 5 English vowels (&#8216;a&#8217;,&nbsp;&#8216;e&#8217;,&nbsp;&#8216;i&#8217;,&nbsp;&#8216;o&#8217;,&nbsp;&#8216;u&#8217;) must appear&nbsp;at least once&nbsp;in it. The letters must be&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[177,4],"class_list":["post-8399","post","type-post","status-publish","format-standard","hentry","category-string","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8399","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=8399"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8399\/revisions"}],"predecessor-version":[{"id":8401,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8399\/revisions\/8401"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}