{"id":6814,"date":"2020-05-23T22:28:48","date_gmt":"2020-05-24T05:28:48","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6814"},"modified":"2020-05-23T22:30:09","modified_gmt":"2020-05-24T05:30:09","slug":"leetcode-1456-maximum-number-of-vowels-in-a-substring-of-given-length","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/sliding-window\/leetcode-1456-maximum-number-of-vowels-in-a-substring-of-given-length\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1456. Maximum Number of Vowels in a Substring of Given Length"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>s<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the maximum number of vowel letters<\/em>&nbsp;in any substring of&nbsp;<code>s<\/code>&nbsp;with&nbsp;length&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p><strong>Vowel letters<\/strong>&nbsp;in&nbsp;English are&nbsp;(a, e, i, o, u).<\/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 = \"abciiidef\", k = 3\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> The substring \"iii\" contains 3 vowel letters.\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 = \"aeiou\", k = 2\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> Any substring of length 2 contains 2 vowels.\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 = \"leetcode\", k = 3\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> \"lee\", \"eet\" and \"ode\" contain 2 vowels.\n<\/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> s = \"rhythms\", k = 4\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> We can see that s doesn't have any vowel letters.\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"tryhard\", k = 4\n<strong>Output:<\/strong> 1\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;= 10^5<\/code><\/li><li><code>s<\/code>&nbsp;consists of lowercase English letters.<\/li><li><code>1 &lt;= k &lt;= s.length<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sliding Window<\/strong><\/h2>\n\n\n\n<p>Keep tracking the number of vows in a window of size k.<\/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 maxVowels(string s, int k) {    \n    vector<int> v(128);\n    v['a'] = v['e'] = v['i'] = v['o'] = v['u'] = 1;\n    int total = 0;\n    int ans = 0;\n    for (int i = 1; i <= s.length(); ++i) {\n      total += v[s[i - 1]];\n      if (i >= k) {\n        ans = max(ans, total);\n        total -= v[s[i - k]];\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s&nbsp;and an integer&nbsp;k. Return&nbsp;the maximum number of vowel letters&nbsp;in any substring of&nbsp;s&nbsp;with&nbsp;length&nbsp;k. Vowel letters&nbsp;in&nbsp;English are&nbsp;(a, e, i, o, u). Example 1: Input: s&#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":[177,215,4,609],"class_list":["post-6814","post","type-post","status-publish","format-standard","hentry","category-sliding-window","tag-medium","tag-sliding-window","tag-string","tag-vows","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6814","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=6814"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6814\/revisions"}],"predecessor-version":[{"id":6816,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6814\/revisions\/6816"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}