{"id":8679,"date":"2021-11-07T08:14:40","date_gmt":"2021-11-07T16:14:40","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8679"},"modified":"2021-11-07T08:15:37","modified_gmt":"2021-11-07T16:15:37","slug":"leetcode-2063-vowels-of-all-substrings","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2063-vowels-of-all-substrings\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2063. Vowels of All Substrings"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>word<\/code>, return&nbsp;<em>the&nbsp;<strong>sum of the number of vowels<\/strong>&nbsp;(<\/em><code>'a'<\/code>,&nbsp;<code>'e'<\/code><em>,<\/em>&nbsp;<code>'i'<\/code><em>,<\/em>&nbsp;<code>'o'<\/code><em>, and<\/em>&nbsp;<code>'u'<\/code><em>)<\/em>&nbsp;<em>in every substring of&nbsp;<\/em><code>word<\/code>.<\/p>\n\n\n\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><strong>Note:<\/strong>&nbsp;Due to the large constraints, the answer may not fit in a signed 32-bit integer. Please be careful during the calculations.<\/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 = \"aba\"\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> \nAll possible substrings are: \"a\", \"ab\", \"aba\", \"b\", \"ba\", and \"a\".\n- \"b\" has 0 vowels in it\n- \"a\", \"ab\", \"ba\", and \"a\" have 1 vowel each\n- \"aba\" has 2 vowels in it\nHence, the total sum of vowels = 0 + 1 + 1 + 1 + 1 + 2 = 6. \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 = \"abc\"\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> \nAll possible substrings are: \"a\", \"ab\", \"abc\", \"b\", \"bc\", and \"c\".\n- \"a\", \"ab\", and \"abc\" have 1 vowel each\n- \"b\", \"bc\", and \"c\" have 0 vowels each\nHence, the total sum of vowels = 1 + 1 + 1 + 0 + 0 + 0 = 3. <\/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 = \"ltcd\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> There are no vowels in any substring of \"ltcd\".<\/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 = \"noosabasboosa\"\n<strong>Output:<\/strong> 237\n<strong>Explanation:<\/strong> There are a total of 237 vowels in all the 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;= 10<sup>5<\/sup><\/code><\/li><li><code>word<\/code>&nbsp;consists of lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>For a vowel at index i, <br>we can choose 0, 1, &#8230; i as starting point<br>choose i, i+1, &#8230;, n -1 as end point.<br>There will be (i &#8211; 0 + 1) * (n &#8211; 1 &#8211; i + 1) possible substrings that contains word[i].<\/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++\">\nclass Solution {\npublic:\n  long long countVowels(string word) {\n    const long long n = word.size();\n    long long ans = 0;\n    for (long long i = 0; i < n; ++i) {\n      switch (word[i]) {\n        case 'a':\n        case 'e':\n        case 'i':\n        case 'o':\n        case 'u':\n          ans += (i + 1) * (n - 1 - i + 1);\n          break;\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;word, return&nbsp;the&nbsp;sum of the number of vowels&nbsp;(&#8216;a&#8217;,&nbsp;&#8216;e&#8217;,&nbsp;&#8216;i&#8217;,&nbsp;&#8216;o&#8217;, and&nbsp;&#8216;u&#8217;)&nbsp;in every substring of&nbsp;word. A&nbsp;substring&nbsp;is a contiguous (non-empty) sequence of characters within a string. Note:&nbsp;Due to&#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":[31,177,4,725],"class_list":["post-8679","post","type-post","status-publish","format-standard","hentry","category-string","tag-math","tag-medium","tag-string","tag-vowels","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8679","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=8679"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8679\/revisions"}],"predecessor-version":[{"id":8681,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8679\/revisions\/8681"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}