{"id":9613,"date":"2022-04-02T18:48:46","date_gmt":"2022-04-03T01:48:46","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9613"},"modified":"2022-04-02T18:49:19","modified_gmt":"2022-04-03T01:49:19","slug":"leetcode-2223-sum-of-scores-of-built-strings","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2223-sum-of-scores-of-built-strings\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2223. Sum of Scores of Built Strings"},"content":{"rendered":"\n<p>You are&nbsp;<strong>building<\/strong>&nbsp;a string&nbsp;<code>s<\/code>&nbsp;of length&nbsp;<code>n<\/code>&nbsp;<strong>one<\/strong>&nbsp;character at a time,&nbsp;<strong>prepending<\/strong>&nbsp;each new character to the&nbsp;<strong>front<\/strong>&nbsp;of the string. The strings are labeled from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>n<\/code>, where the string with length&nbsp;<code>i<\/code>&nbsp;is labeled&nbsp;<code>s<sub>i<\/sub><\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, for&nbsp;<code>s = \"abaca\"<\/code>,&nbsp;<code>s<sub>1<\/sub>&nbsp;== \"a\"<\/code>,&nbsp;<code>s<sub>2<\/sub>&nbsp;== \"ca\"<\/code>,&nbsp;<code>s<sub>3<\/sub>&nbsp;== \"aca\"<\/code>, etc.<\/li><\/ul>\n\n\n\n<p>The&nbsp;<strong>score<\/strong>&nbsp;of&nbsp;<code>s<sub>i<\/sub><\/code>&nbsp;is the length of the&nbsp;<strong>longest common prefix<\/strong>&nbsp;between&nbsp;<code>s<sub>i<\/sub><\/code>&nbsp;and&nbsp;<code>s<sub>n<\/sub><\/code>&nbsp;(Note that&nbsp;<code>s == s<sub>n<\/sub><\/code>).<\/p>\n\n\n\n<p>Given the final string&nbsp;<code>s<\/code>, return<em>&nbsp;the&nbsp;<strong>sum<\/strong>&nbsp;of the&nbsp;<strong>score<\/strong>&nbsp;of every&nbsp;<\/em><code>s<sub>i<\/sub><\/code>.<\/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 = \"babab\"\n<strong>Output:<\/strong> 9\n<strong>Explanation:<\/strong>\nFor s<sub>1<\/sub> == \"b\", the longest common prefix is \"b\" which has a score of 1.\nFor s<sub>2<\/sub> == \"ab\", there is no common prefix so the score is 0.\nFor s<sub>3<\/sub> == \"bab\", the longest common prefix is \"bab\" which has a score of 3.\nFor s<sub>4<\/sub> == \"abab\", there is no common prefix so the score is 0.\nFor s<sub>5<\/sub> == \"babab\", the longest common prefix is \"babab\" which has a score of 5.\nThe sum of the scores is 1 + 0 + 3 + 0 + 5 = 9, so we return 9.<\/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 = \"azbazbzaz\"\n<strong>Output:<\/strong> 14\n<strong>Explanation:<\/strong> \nFor s<sub>2<\/sub> == \"az\", the longest common prefix is \"az\" which has a score of 2.\nFor s<sub>6<\/sub> == \"azbzaz\", the longest common prefix is \"azb\" which has a score of 3.\nFor s<sub>9<\/sub> == \"azbazbzaz\", the longest common prefix is \"azbazbzaz\" which has a score of 9.\nFor all other s<sub>i<\/sub>, the score is 0.\nThe sum of the scores is 2 + 3 + 9 = 14, so we return 14.\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 English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Z-Function<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/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 sumScores(string s) {\n    auto zFunc = [](string_view s) {\n      const int n = s.length();\n      vector<long long> z(n);\n      for (long long i = 1, l = 0, r = 0; i < n; ++i) {\n        if (i <= r)\n          z[i] = min(r - i + 1, z[i - l]);\n        while (i + z[i] < n &#038;&#038; s[z[i]] == s[i + z[i]])\n          ++z[i];\n        if (i + z[i] - 1 > r) {\n          l = i;\n          r = i + z[i] - 1;\n        }      \n      }\n      return accumulate(begin(z), end(z), 0LL);\n    };    \n    return zFunc(s) + s.length();\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are&nbsp;building&nbsp;a string&nbsp;s&nbsp;of length&nbsp;n&nbsp;one&nbsp;character at a time,&nbsp;prepending&nbsp;each new character to the&nbsp;front&nbsp;of the string. The strings are labeled from&nbsp;1&nbsp;to&nbsp;n, where the string with length&nbsp;i&nbsp;is labeled&nbsp;si. For&#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":[217,4],"class_list":["post-9613","post","type-post","status-publish","format-standard","hentry","category-string","tag-hard","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9613","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=9613"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9613\/revisions"}],"predecessor-version":[{"id":9615,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9613\/revisions\/9615"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}