{"id":6400,"date":"2020-03-08T00:00:30","date_gmt":"2020-03-08T08:00:30","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6400"},"modified":"2020-03-10T19:29:35","modified_gmt":"2020-03-11T02:29:35","slug":"leetcode-1371-find-the-longest-substring-containing-vowels-in-even-counts","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1371-find-the-longest-substring-containing-vowels-in-even-counts\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1371. Find the Longest Substring Containing Vowels in Even Counts"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1371. Find the Longest Substring Containing Vowels in Even Counts - \u5237\u9898\u627e\u5de5\u4f5c EP312\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/tAlQxFvak2U?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given the string&nbsp;<code>s<\/code>, return the size of the longest substring containing each vowel an even number of times. That is, &#8216;a&#8217;, &#8216;e&#8217;, &#8216;i&#8217;, &#8216;o&#8217;, and &#8216;u&#8217; must appear an even number of times.<\/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> s = \"eleetminicoworoep\"\n<strong>Output:<\/strong> 13\n<strong>Explanation: <\/strong>The longest substring is \"leetminicowor\" which contains two each of the vowels: <strong>e<\/strong>, <strong>i<\/strong> and <strong>o<\/strong> and zero of the vowels: <strong>a<\/strong> and <strong>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> s = \"leetcodeisgreat\"\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> The longest substring is \"leetc\" which contains two e's.\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> s = \"bcbcbc\"\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> In this case, the given string \"bcbcbc\" is the longest because all vowels: <strong>a<\/strong>, <strong>e<\/strong>, <strong>i<\/strong>, <strong>o<\/strong> and <strong>u<\/strong> appear zero times.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 5 x 10^5<\/code><\/li><li><code>s<\/code>&nbsp;contains only lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: HashTable<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/03\/1371-ep312.png\" alt=\"\" class=\"wp-image-6460\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/03\/1371-ep312.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/03\/1371-ep312-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/03\/1371-ep312-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Record the first index when a state occurs. index &#8211; last_index is the length of the all-even-vowel substring.<\/p>\n\n\n\n<p>State: {a: odd|even,  e: odd|even, &#8230;, u:odd|even}. <\/p>\n\n\n\n<p>There are total 2^5 = 32 states that can be represented as a binary string.<\/p>\n\n\n\n<p>whenever a vowel occurs, we flip the bit, e.g. odd-&gt;even, even-&gt;odd using XOR.<\/p>\n\n\n\n<p>Time complexity: O(5*n)<br>Space complexity: O(32)<\/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 findTheLongestSubstring(string s) {\n    const char vowels[] = \"aeiou\";\n    vector<int> idx(1 << 5, INT_MAX); \/\/ state -> first_index    \n    idx[0] = -1;\n    int state = 0;\n    int ans = 0;\n    for (int i = 0; i < s.length(); ++i) {\n      for (int j = 0; j < 5; ++j)\n        if (s[i] == vowels[j]) state ^= 1 << j;\n      if (idx[state] == INT_MAX) idx[state] = i;\n      ans = max(ans, i - idx[state]);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def findTheLongestSubstring(self, s: str) -> int:\n    idx = {0 : -1}\n    vowels = \"aeiou\"\n    state = 0\n    ans = 0\n    for i in range(len(s)):\n      j = vowels.find(s[i])\n      if j >= 0: state ^= 1 << j\n      if state not in idx:\n        idx[state] = i\n      ans = max(ans, i - idx[state])\n    return ans\n\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given the string&nbsp;s, return the size of the longest substring containing each vowel an even number of times. That is, &#8216;a&#8217;, &#8216;e&#8217;, &#8216;i&#8217;, &#8216;o&#8217;, and&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[82,177,4,314],"class_list":["post-6400","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-medium","tag-string","tag-substring","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6400","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=6400"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6400\/revisions"}],"predecessor-version":[{"id":6461,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6400\/revisions\/6461"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}