{"id":8499,"date":"2021-08-06T23:16:41","date_gmt":"2021-08-07T06:16:41","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8499"},"modified":"2021-08-06T23:17:08","modified_gmt":"2021-08-07T06:17:08","slug":"leetcode-1876-substrings-of-size-three-with-distinct-characters","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1876-substrings-of-size-three-with-distinct-characters\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1876. Substrings of Size Three with Distinct Characters"},"content":{"rendered":"\n<p>A string is&nbsp;<strong>good<\/strong>&nbsp;if there are no repeated characters.<\/p>\n\n\n\n<p>Given a string&nbsp;<code>s<\/code>\u200b\u200b\u200b\u200b\u200b, return&nbsp;<em>the number of&nbsp;<strong>good substrings<\/strong>&nbsp;of length&nbsp;<strong>three&nbsp;<\/strong>in&nbsp;<\/em><code>s<\/code>\u200b\u200b\u200b\u200b\u200b\u200b.<\/p>\n\n\n\n<p>Note that if there are multiple occurrences of the same substring, every occurrence should be counted.<\/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> s = \"xyzzaz\"\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> There are 4 substrings of size 3: \"xyz\", \"yzz\", \"zza\", and \"zaz\". \nThe only good substring of length 3 is \"xyz\".\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 = \"aababcabc\"\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> There are 7 substrings of size 3: \"aab\", \"aba\", \"bab\", \"abc\", \"bca\", \"cab\", and \"abc\".\nThe good substrings are \"abc\", \"bca\", \"cab\", and \"abc\".\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;= 100<\/code><\/li><li><code>s<\/code>\u200b\u200b\u200b\u200b\u200b\u200b consists of lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force w\/ (Hash)Set<\/strong><\/h2>\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  int countGoodSubstrings(string s) {\n    int ans = 0;\n    for (int i = 0; i + 2 < s.length(); ++i) \n      ans += set<char>(s.begin() + i, s.begin() + i + 3).size() == 3;\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\nclass Solution:\n  def countGoodSubstrings(self, s: str) -> int:    \n    return sum(len(set(s[i:i + 3])) == 3 for i in range(len(s) - 2))\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A string is&nbsp;good&nbsp;if there are no repeated characters. Given a string&nbsp;s\u200b\u200b\u200b\u200b\u200b, return&nbsp;the number of&nbsp;good substrings&nbsp;of length&nbsp;three&nbsp;in&nbsp;s\u200b\u200b\u200b\u200b\u200b\u200b. Note that if there are multiple occurrences of the&#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":[222,299,4],"class_list":["post-8499","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-hashset","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8499","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=8499"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8499\/revisions"}],"predecessor-version":[{"id":8502,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8499\/revisions\/8502"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}