{"id":8105,"date":"2021-02-13T20:55:02","date_gmt":"2021-02-14T04:55:02","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8105"},"modified":"2021-02-13T20:55:58","modified_gmt":"2021-02-14T04:55:58","slug":"leetcode-1759-count-number-of-homogenous-substrings","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1759-count-number-of-homogenous-substrings\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1759. Count Number of Homogenous Substrings"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>s<\/code>, return&nbsp;<em>the number of&nbsp;<strong>homogenous<\/strong>&nbsp;substrings of&nbsp;<\/em><code>s<\/code><em>.<\/em>&nbsp;Since the answer may be too large, return it&nbsp;<strong>modulo<\/strong>&nbsp;<code>10<sup>9<\/sup>&nbsp;+ 7<\/code>.<\/p>\n\n\n\n<p>A string is&nbsp;<strong>homogenous<\/strong>&nbsp;if all the characters of the string are the same.<\/p>\n\n\n\n<p>A&nbsp;<strong>substring<\/strong>&nbsp;is a contiguous sequence of characters within 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 = \"abbcccaa\"\n<strong>Output:<\/strong> 13\n<strong>Explanation:<\/strong> The homogenous substrings are listed as below:\n\"a\"   appears 3 times.\n\"aa\"  appears 1 time.\n\"b\"   appears 2 times.\n\"bb\"  appears 1 time.\n\"c\"   appears 3 times.\n\"cc\"  appears 2 times.\n\"ccc\" appears 1 time.\n3 + 1 + 2 + 1 + 3 + 2 + 1 = 13.<\/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 = \"xy\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The homogenous substrings are \"x\" and \"y\".<\/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 = \"zzzzz\"\n<strong>Output:<\/strong> 15\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<sup>5<\/sup><\/code><\/li><li><code>s<\/code>&nbsp;consists of lowercase letters.<\/li><\/ul>\n\n\n\n<p><strong>Solution: Counting<\/strong><\/p>\n\n\n\n<p>Let m be the length of the longest homogenous substring, # of homogenous substring is m * (m + 1) \/ 2.<br>e.g. aaabb<br>&#8220;aaa&#8221; =&gt; m = 3,  # = 3 * (3 + 1) \/ 2 = 6<br>&#8220;bb&#8221; =&gt; m = 2, # = 2 * (2+1) \/ 2 = 3<br>Total = 6 + 3 = 9<\/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 countHomogenous(string s) {\n    constexpr int kMod = 1e9 + 7;\n    const int n = s.length();\n    long ans = 0;\n    for (long i = 0, j = 0; i < n; i = j) {\n      while (j < n &#038;&#038; s[i] == s[j]) ++j;\n      ans += (j - i) * (j - i + 1) \/ 2;\n    }\n    return ans % kMod;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s, return&nbsp;the number of&nbsp;homogenous&nbsp;substrings of&nbsp;s.&nbsp;Since the answer may be too large, return it&nbsp;modulo&nbsp;109&nbsp;+ 7. A string is&nbsp;homogenous&nbsp;if all the characters of the string&#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":[8,177,4],"class_list":["post-8105","post","type-post","status-publish","format-standard","hentry","category-string","tag-counting","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8105","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=8105"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8105\/revisions"}],"predecessor-version":[{"id":8107,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8105\/revisions\/8107"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}