{"id":9676,"date":"2022-04-17T15:33:20","date_gmt":"2022-04-17T22:33:20","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9676"},"modified":"2022-04-17T15:34:42","modified_gmt":"2022-04-17T22:34:42","slug":"leetcode-2243-calculate-digit-sum-of-a-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2243-calculate-digit-sum-of-a-string\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2243. Calculate Digit Sum of a String"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>s<\/code>&nbsp;consisting of digits and an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>A&nbsp;<strong>round<\/strong>&nbsp;can be completed if the length of&nbsp;<code>s<\/code>&nbsp;is greater than&nbsp;<code>k<\/code>. In one round, do the following:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Divide<\/strong>&nbsp;<code>s<\/code>&nbsp;into&nbsp;<strong>consecutive groups<\/strong>&nbsp;of size&nbsp;<code>k<\/code>&nbsp;such that the first&nbsp;<code>k<\/code>&nbsp;characters are in the first group, the next&nbsp;<code>k<\/code>&nbsp;characters are in the second group, and so on.&nbsp;<strong>Note<\/strong>&nbsp;that the size of the last group can be smaller than&nbsp;<code>k<\/code>.<\/li><li><strong>Replace<\/strong>&nbsp;each group of&nbsp;<code>s<\/code>&nbsp;with a string representing the sum of all its digits. For example,&nbsp;<code>\"346\"<\/code>&nbsp;is replaced with&nbsp;<code>\"13\"<\/code>&nbsp;because&nbsp;<code>3 + 4 + 6 = 13<\/code>.<\/li><li><strong>Merge<\/strong>&nbsp;consecutive groups together to form a new string. If the length of the string is greater than&nbsp;<code>k<\/code>, repeat from step&nbsp;<code>1<\/code>.<\/li><\/ol>\n\n\n\n<p>Return&nbsp;<code>s<\/code>&nbsp;<em>after all rounds have been completed<\/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 = \"11111222223\", k = 3\n<strong>Output:<\/strong> \"135\"\n<strong>Explanation:<\/strong> \n- For the first round, we divide s into groups of size 3: \"111\", \"112\", \"222\", and \"23\".\n  \u200b\u200b\u200b\u200b\u200bThen we calculate the digit sum of each group: 1 + 1 + 1 = 3, 1 + 1 + 2 = 4, 2 + 2 + 2 = 6, and 2 + 3 = 5. \n&nbsp; So, s becomes \"3\" + \"4\" + \"6\" + \"5\" = \"3465\" after the first round.\n- For the second round, we divide s into \"346\" and \"5\".\n&nbsp; Then we calculate the digit sum of each group: 3 + 4 + 6 = 13, 5 = 5. \n&nbsp; So, s becomes \"13\" + \"5\" = \"135\" after second round. \nNow, s.length &lt;= k, so we return \"135\" as the answer.\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 = \"00000000\", k = 3\n<strong>Output:<\/strong> \"000\"\n<strong>Explanation:<\/strong> \nWe divide s into \"000\", \"000\", and \"00\".\nThen we calculate the digit sum of each group: 0 + 0 + 0 = 0, 0 + 0 + 0 = 0, and 0 + 0 = 0. \ns becomes \"0\" + \"0\" + \"0\" = \"000\", whose length is equal to k, so we return \"000\".\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>2 &lt;= k &lt;= 100<\/code><\/li><li><code>s<\/code>&nbsp;consists of digits only.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: <\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n*n\/k?)<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  string digitSum(string s, int k) {\n    while (s.length() > k) {\n      string ss;\n      for (int j = 0; j < s.length(); j += k) {\n        int sum = 0;\n        for (int i = 0; i < k &#038;&#038; i + j < s.length(); ++i)\n          sum += (s[j + i] - '0');\n        ss += to_string(sum);\n      }\n      ss.swap(s);\n    }\n    return s;            \n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;s&nbsp;consisting of digits and an integer&nbsp;k. A&nbsp;round&nbsp;can be completed if the length of&nbsp;s&nbsp;is greater than&nbsp;k. In one round, do the following:&#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,4],"class_list":["post-9676","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9676","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=9676"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9676\/revisions"}],"predecessor-version":[{"id":9678,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9676\/revisions\/9678"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}