{"id":9311,"date":"2021-12-31T12:23:55","date_gmt":"2021-12-31T20:23:55","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9311"},"modified":"2021-12-31T12:24:25","modified_gmt":"2021-12-31T20:24:25","slug":"leetcode-1945-sum-of-digits-of-string-after-convert","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1945-sum-of-digits-of-string-after-convert\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1945. Sum of Digits of String After Convert"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>s<\/code>&nbsp;consisting of lowercase English letters, and an integer&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>First,&nbsp;<strong>convert<\/strong>&nbsp;<code>s<\/code>&nbsp;into an integer by replacing each letter with its position in the alphabet (i.e., replace&nbsp;<code>'a'<\/code>&nbsp;with&nbsp;<code>1<\/code>,&nbsp;<code>'b'<\/code>&nbsp;with&nbsp;<code>2<\/code>, &#8230;,&nbsp;<code>'z'<\/code>&nbsp;with&nbsp;<code>26<\/code>). Then,&nbsp;<strong>transform<\/strong>&nbsp;the integer by replacing it with the&nbsp;<strong>sum of its digits<\/strong>. Repeat the&nbsp;<strong>transform<\/strong>&nbsp;operation&nbsp;<code>k<\/code><strong>&nbsp;times<\/strong>&nbsp;in total.<\/p>\n\n\n\n<p>For example, if&nbsp;<code>s = \"zbax\"<\/code>&nbsp;and&nbsp;<code>k = 2<\/code>, then the resulting integer would be&nbsp;<code>8<\/code>&nbsp;by the following operations:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Convert<\/strong>:&nbsp;<code>\"zbax\" \u279d \"(26)(2)(1)(24)\" \u279d \"262124\" \u279d 262124<\/code><\/li><li><strong>Transform #1<\/strong>:&nbsp;<code>262124 \u279d 2 + 6 + 2 + 1 + 2 + 4&nbsp;\u279d 17<\/code><\/li><li><strong>Transform #2<\/strong>:&nbsp;<code>17 \u279d 1 + 7 \u279d 8<\/code><\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the resulting integer after performing the operations described above<\/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 = \"iiii\", k = 1\n<strong>Output:<\/strong> 36\n<strong>Explanation:<\/strong> The operations are as follows:\n- Convert: \"iiii\" \u279d \"(9)(9)(9)(9)\" \u279d \"9999\" \u279d 9999\n- Transform #1: 9999 \u279d 9 + 9 + 9 + 9 \u279d 36\nThus the resulting integer is 36.\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 = \"leetcode\", k = 2\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> The operations are as follows:\n- Convert: \"leetcode\" \u279d \"(12)(5)(5)(20)(3)(15)(4)(5)\" \u279d \"12552031545\" \u279d 12552031545\n- Transform #1: 12552031545 \u279d 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 \u279d 33\n- Transform #2: 33 \u279d 3 + 3 \u279d 6\nThus the resulting integer is 6.\n<\/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 = \"zbax\", k = 2\n<strong>Output:<\/strong> 8\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>1 &lt;= k &lt;= 10<\/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: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(klogn)<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++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  int getLucky(string s, int k) {\n    int ans = 0;\n    for (char c : s) {\n      int n = c - 'a' + 1;\n      ans += n % 10;\n      ans += n \/ 10;\n    }\n    while (--k) {\n      int n = ans;\n      ans = 0;\n      while (n) {\n        ans += n % 10;\n        n \/= 10;\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;s&nbsp;consisting of lowercase English letters, and an integer&nbsp;k. First,&nbsp;convert&nbsp;s&nbsp;into an integer by replacing each letter with its position in the alphabet&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[695,222,179,4],"class_list":["post-9311","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-base10","tag-easy","tag-simulation","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9311","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=9311"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9311\/revisions"}],"predecessor-version":[{"id":9313,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9311\/revisions\/9313"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}