{"id":6360,"date":"2020-02-22T23:41:07","date_gmt":"2020-02-23T07:41:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6360"},"modified":"2020-02-22T23:46:25","modified_gmt":"2020-02-23T07:46:25","slug":"1358-number-of-substrings-containing-all-three-characters","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/1358-number-of-substrings-containing-all-three-characters\/","title":{"rendered":"\u82b1\u82b1\u9171 1358. Number of Substrings Containing All Three Characters"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>s<\/code>&nbsp;consisting only of characters&nbsp;<em>a<\/em>,&nbsp;<em>b<\/em>&nbsp;and&nbsp;<em>c<\/em>.<\/p>\n\n\n\n<p>Return the number of substrings containing&nbsp;<strong>at least<\/strong>&nbsp;one occurrence of all these characters&nbsp;<em>a<\/em>,&nbsp;<em>b<\/em>&nbsp;and&nbsp;<em>c<\/em>.<\/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 = \"abcabc\"\n<strong>Output:<\/strong> 10\n<strong>Explanation:<\/strong> The substrings containing&nbsp;at least&nbsp;one occurrence of the characters&nbsp;<em>a<\/em>,&nbsp;<em>b<\/em>&nbsp;and&nbsp;<em>c are \"<\/em>abc<em>\", \"<\/em>abca<em>\", \"<\/em>abcab<em>\", \"<\/em>abcabc<em>\", \"<\/em>bca<em>\", \"<\/em>bcab<em>\", \"<\/em>bcabc<em>\", \"<\/em>cab<em>\", \"<\/em>cabc<em>\" <\/em>and<em> \"<\/em>abc<em>\" <\/em>(<strong>again<\/strong>)<em>. <\/em>\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 = \"aaacb\"\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> The substrings containing&nbsp;at least&nbsp;one occurrence of the characters&nbsp;<em>a<\/em>,&nbsp;<em>b<\/em>&nbsp;and&nbsp;<em>c are \"<\/em>aaacb<em>\", \"<\/em>aacb<em>\" <\/em>and<em> \"<\/em>acb<em>\".<\/em>\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 = \"abc\"\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>3 &lt;= s.length &lt;= 5 x 10^4<\/code><\/li><li><code>s<\/code>&nbsp;only consists of&nbsp;<em>a<\/em>,&nbsp;<em>b<\/em>&nbsp;or&nbsp;<em>c&nbsp;<\/em>characters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution<\/strong><\/h2>\n\n\n\n<p>Record the last index of each character. <\/p>\n\n\n\n<p>At each index i, we can choose any index j that j &lt;= min(last_a, last_b, last_c) as the starting point, and there will be  min(last_a, last_b, last_c) + 1 valid substrings.<\/p>\n\n\n\n<p>e.g. aabbabcc&#8230;<br>last_a = 4<br>last_b = 5<br>last_c = 7<br>min(last_a, last_b, last_c) = 4<br>aabba | bcc<br>We can choose any char with index &lt;= 4 as string point, there are 5 of them:<br>aabbabcc<br>abbabcc<br>bbabcc<br>babcc<br>abcc<\/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 numberOfSubstrings(string s) {\n    vector<int> l{-1,-1,-1};\n    int ans = 0;\n    for (size_t i = 0; i < s.length(); ++i) {\n      l[s[i] - 'a'] = i;\n      ans += 1 + *min_element(begin(l), end(l));\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\"># Author: Huahua\nclass Solution:\n  def numberOfSubstrings(self, s: str) -> int:\n    l = {'a':-1, 'b':-1, 'c':-1}\n    ans = 0\n    for i, ch in enumerate(s):\n      l[ch] = i\n      ans += min(l.values()) + 1\n    return ans\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s&nbsp;consisting only of characters&nbsp;a,&nbsp;b&nbsp;and&nbsp;c. Return the number of substrings containing&nbsp;at least&nbsp;one occurrence of all these characters&nbsp;a,&nbsp;b&nbsp;and&nbsp;c. Example 1: Input: s = &#8220;abcabc&#8221; Output:&#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":[82,177,314],"class_list":["post-6360","post","type-post","status-publish","format-standard","hentry","category-string","tag-hashtable","tag-medium","tag-substring","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6360","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=6360"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6360\/revisions"}],"predecessor-version":[{"id":6363,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6360\/revisions\/6363"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}