|
| 1 | +<h2><a href="https://leetcode.com/problems/sum-of-digits-of-string-after-convert/">2076. Sum of Digits of String After Convert</a></h2><h3>Easy</h3><hr><p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>. Your task is to <em>convert</em> the string into an integer by a special process, and then <em>transform</em> it by summing its digits repeatedly <code>k</code> times. More specifically, perform the following steps:</p> |
| 2 | + |
| 3 | +<ol> |
| 4 | + <li><strong>Convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e. replace <code>'a'</code> with <code>1</code>, <code>'b'</code> with <code>2</code>, ..., <code>'z'</code> with <code>26</code>).</li> |
| 5 | + <li><strong>T</strong><strong>ransform</strong> the integer by replacing it with the <strong>sum of its digits</strong>.</li> |
| 6 | + <li>Repeat the <strong>transform</strong> operation (step 2) <code>k</code><strong> times</strong> in total.</li> |
| 7 | +</ol> |
| 8 | + |
| 9 | +<p>For example, if <code>s = "zbax"</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p> |
| 10 | + |
| 11 | +<ol> |
| 12 | + <li><strong>Convert</strong>: <code>"zbax" ➝ "(26)(2)(1)(24)" ➝ "262124" ➝ 262124</code></li> |
| 13 | + <li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4 ➝ 17</code></li> |
| 14 | + <li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li> |
| 15 | +</ol> |
| 16 | + |
| 17 | +<p>Return the <strong>resulting</strong> <strong>integer</strong> after performing the <strong>operations</strong> described above.</p> |
| 18 | + |
| 19 | +<p> </p> |
| 20 | +<p><strong class="example">Example 1:</strong></p> |
| 21 | + |
| 22 | +<div class="example-block"> |
| 23 | +<p><strong>Input:</strong> <span class="example-io">s = "iiii", k = 1</span></p> |
| 24 | + |
| 25 | +<p><strong>Output:</strong> <span class="example-io">36</span></p> |
| 26 | + |
| 27 | +<p><strong>Explanation:</strong></p> |
| 28 | + |
| 29 | +<p>The operations are as follows:<br /> |
| 30 | +- Convert: "iiii" ➝ "(9)(9)(9)(9)" ➝ "9999" ➝ 9999<br /> |
| 31 | +- Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36<br /> |
| 32 | +Thus the resulting integer is 36.</p> |
| 33 | +</div> |
| 34 | + |
| 35 | +<p><strong class="example">Example 2:</strong></p> |
| 36 | + |
| 37 | +<div class="example-block"> |
| 38 | +<p><strong>Input:</strong> <span class="example-io">s = "leetcode", k = 2</span></p> |
| 39 | + |
| 40 | +<p><strong>Output:</strong> <span class="example-io">6</span></p> |
| 41 | + |
| 42 | +<p><strong>Explanation:</strong></p> |
| 43 | + |
| 44 | +<p>The operations are as follows:<br /> |
| 45 | +- Convert: "leetcode" ➝ "(12)(5)(5)(20)(3)(15)(4)(5)" ➝ "12552031545" ➝ 12552031545<br /> |
| 46 | +- Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33<br /> |
| 47 | +- Transform #2: 33 ➝ 3 + 3 ➝ 6<br /> |
| 48 | +Thus the resulting integer is 6.</p> |
| 49 | +</div> |
| 50 | + |
| 51 | +<p><strong class="example">Example 3:</strong></p> |
| 52 | + |
| 53 | +<div class="example-block"> |
| 54 | +<p><strong>Input:</strong> <span class="example-io">s = "zbax", k = 2</span></p> |
| 55 | + |
| 56 | +<p><strong>Output:</strong> <span class="example-io">8</span></p> |
| 57 | +</div> |
| 58 | + |
| 59 | +<p> </p> |
| 60 | +<p><strong>Constraints:</strong></p> |
| 61 | + |
| 62 | +<ul> |
| 63 | + <li><code>1 <= s.length <= 100</code></li> |
| 64 | + <li><code>1 <= k <= 10</code></li> |
| 65 | + <li><code>s</code> consists of lowercase English letters.</li> |
| 66 | +</ul> |
0 commit comments