{"id":9230,"date":"2021-12-25T03:11:08","date_gmt":"2021-12-25T11:11:08","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9230"},"modified":"2021-12-30T03:05:55","modified_gmt":"2021-12-30T11:05:55","slug":"leetcode-1915-number-of-wonderful-substrings","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1915-number-of-wonderful-substrings\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1915. Number of Wonderful Substrings"},"content":{"rendered":"\n<p>A&nbsp;<strong>wonderful<\/strong>&nbsp;string is a string where&nbsp;<strong>at most one<\/strong>&nbsp;letter appears an&nbsp;<strong>odd<\/strong>&nbsp;number of times.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example,&nbsp;<code>\"ccjjc\"<\/code>&nbsp;and&nbsp;<code>\"abab\"<\/code>&nbsp;are wonderful, but&nbsp;<code>\"ab\"<\/code>&nbsp;is not.<\/li><\/ul>\n\n\n\n<p>Given a string&nbsp;<code>word<\/code>&nbsp;that consists of the first ten lowercase English letters (<code>'a'<\/code>&nbsp;through&nbsp;<code>'j'<\/code>), return&nbsp;<em>the&nbsp;<strong>number of wonderful non-empty substrings<\/strong>&nbsp;in&nbsp;<\/em><code>word<\/code><em>. If the same substring appears multiple times in&nbsp;<\/em><code>word<\/code><em>, then count&nbsp;<strong>each occurrence<\/strong>&nbsp;separately.<\/em><\/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 = \"aba\"\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> The four wonderful substrings are underlined below:\n- \"<strong>a<\/strong>ba\" -&gt; \"a\"\n- \"a<strong>b<\/strong>a\" -&gt; \"b\"\n- \"ab<strong>a<\/strong>\" -&gt; \"a\"\n- \"<strong>aba<\/strong>\" -&gt; \"aba\"\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 = \"aabb\"\n<strong>Output:<\/strong> 9\n<strong>Explanation:<\/strong> The nine wonderful substrings are underlined below:\n- \"<strong><u>a<\/u><\/strong>abb\" -&gt; \"a\"\n- \"<strong>aa<\/strong>bb\" -&gt; \"aa\"\n- \"<strong>aab<\/strong>b\" -&gt; \"aab\"\n- \"<strong>aabb<\/strong>\" -&gt; \"aabb\"\n- \"a<strong>a<\/strong>bb\" -&gt; \"a\"\n- \"a<strong>abb<\/strong>\" -&gt; \"abb\"\n- \"aa<strong>b<\/strong>b\" -&gt; \"b\"\n- \"aa<strong>bb<\/strong>\" -&gt; \"bb\"\n- \"aab<strong>b<\/strong>\" -&gt; \"b\"\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 = \"he\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The two wonderful substrings are underlined below:\n- \"<strong><u>h<\/u><\/strong>e\" -&gt; \"h\"\n- \"h<strong><u>e<\/u><\/strong>\" -&gt; \"e\"\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 from&nbsp;<code>'a'<\/code>&nbsp;to&nbsp;<code>'j'<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Prefix Bitmask + Hashtable<\/strong><\/h2>\n\n\n\n<p>Similar to <a href=\"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-1371-find-the-longest-substring-containing-vowels-in-even-counts\/\" data-type=\"post\" data-id=\"6400\">\u82b1\u82b1\u9171 LeetCode 1371. Find the Longest Substring Containing Vowels in Even Counts<\/a>, we use   a bitmask to represent the occurrence (odd or even) of each letter and use a hashtable to store the frequency of each bitmask seen so far.<br><br>1. &#8220;0000000000&#8221; means all letters occur even times.<br>2. &#8220;0000000101&#8221; means <meta charset=\"utf-8\">all letters occur even times expect letter &#8216;a&#8217; and &#8216;c&#8217; that occur odd times.<\/p>\n\n\n\n<p>We scan the word from left to right and update the bitmask: bitmask ^= (1 &lt;&lt; (c-&#8216;a&#8217;)). <br>However, the bitmask only represents the state of the prefix, i.e. word[0:i], then how can we count substrings? The answer is hashtable.  If the same bitmask occurs c times before, which means there are c indices that word[0~j1], word[0~j2], &#8230;, word[0~jc] have the same state as word[0~i] that means for word[j1+1~i], word[j2+1~i], &#8230;, word[jc+1~i], all letters occurred even times. <br>For the &#8220;at most one odd&#8221; case, we toggle each bit of the bitmask and check how many times it occurred before.<\/p>\n\n\n\n<p>ans += freq[mask] + sum(freq[mask ^ (1 &lt;&lt; i)] for i in range(k))<\/p>\n\n\n\n<p>Time complexity: O(n*k)<br>Space complexity: O(2<sup>k<\/sup>)<br>where k = j &#8211; a + 1 = 10<\/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  long long wonderfulSubstrings(string word) {\n    constexpr int l = 'j' - 'a' + 1;\n    vector<int> count(1 << l);\n    count[0] = 1;\n    int mask = 0;\n    long long ans = 0;\n    for (char c : word) {\n      mask ^= 1 << (c - 'a'); \n      for (int i = 0; i < l; ++i)\n        ans += count[mask ^ (1 << i)]; \/\/ one odd.\n      ans += count[mask]++; \/\/ all even.\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A&nbsp;wonderful&nbsp;string is a string where&nbsp;at most one&nbsp;letter appears an&nbsp;odd&nbsp;number of times. For example,&nbsp;&#8220;ccjjc&#8221;&nbsp;and&nbsp;&#8220;abab&#8221;&nbsp;are wonderful, but&nbsp;&#8220;ab&#8221;&nbsp;is not. Given a string&nbsp;word&nbsp;that consists of the first ten lowercase&#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":[621,82,97,314],"class_list":["post-9230","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-bitmask","tag-hashtable","tag-prefix","tag-substring","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9230","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=9230"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9230\/revisions"}],"predecessor-version":[{"id":9284,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9230\/revisions\/9284"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}