{"id":8200,"date":"2021-03-06T09:41:22","date_gmt":"2021-03-06T17:41:22","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8200"},"modified":"2021-03-06T09:42:17","modified_gmt":"2021-03-06T17:42:17","slug":"leetcode-1781-sum-of-beauty-of-all-substrings","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1781-sum-of-beauty-of-all-substrings\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1781. Sum of Beauty of All Substrings"},"content":{"rendered":"\n<p>The&nbsp;<strong>beauty<\/strong>&nbsp;of a string is the difference in frequencies between the most frequent and least frequent characters.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, the beauty of&nbsp;<code>\"abaacc\"<\/code>&nbsp;is&nbsp;<code>3 - 1 = 2<\/code>.<\/li><\/ul>\n\n\n\n<p>Given a string&nbsp;<code>s<\/code>, return&nbsp;<em>the sum of&nbsp;<strong>beauty<\/strong>&nbsp;of all of its substrings.<\/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 = \"aabcb\"\n<strong>Output:<\/strong> 5\n<strong>Explanation: <\/strong>The substrings with non-zero beauty are [\"aab\",\"aabc\",\"aabcb\",\"abcb\",\"bcb\"], each with beauty equal to 1.<\/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 = \"aabcbaa\"\n<strong>Output:<\/strong> 17\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;=500<\/code><\/li><li><code>s<\/code>&nbsp;consists of only lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Treemap<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>log26)<br>Space complexity: O(26)<\/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 beautySum(string_view s) {\n    const int n = s.size();\n    int ans = 0;\n    vector<int> f(26);\n    map<int, int> m;\n    for (int i = 0; i < n; ++i) {\n      fill(begin(f), end(f), 0);\n      m.clear();\n      for (int j = i; j < n; ++j) {\n        const int c = ++f[s[j] - 'a'];\n        ++m[c];\n        if (c > 1) {\n          auto it = m.find(c - 1);\n          if (--it->second == 0)\n            m.erase(it);\n        }\n        ans += rbegin(m)->first - begin(m)->first;  \n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The&nbsp;beauty&nbsp;of a string is the difference in frequencies between the most frequent and least frequent characters. For example, the beauty of&nbsp;&#8220;abaacc&#8221;&nbsp;is&nbsp;3 &#8211; 1 = 2.&#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":[24,177,4,699],"class_list":["post-8200","post","type-post","status-publish","format-standard","hentry","category-string","tag-frequency","tag-medium","tag-string","tag-treemap","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8200","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=8200"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8200\/revisions"}],"predecessor-version":[{"id":8202,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8200\/revisions\/8202"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}