1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (English)/字符串转化后的各位数字之和(English) [sum-of-digits-of-string-after-convert].html

67 lines
3.0 KiB
HTML

<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>
<ol>
<li><strong>Convert</strong> <code>s</code> into an integer by replacing each letter with its position in the alphabet (i.e.&nbsp;replace <code>&#39;a&#39;</code> with <code>1</code>, <code>&#39;b&#39;</code> with <code>2</code>, ..., <code>&#39;z&#39;</code> with <code>26</code>).</li>
<li><strong>T</strong><strong>ransform</strong> the integer by replacing it with the <strong>sum of its digits</strong>.</li>
<li>Repeat the <strong>transform</strong> operation (step 2) <code>k</code><strong> times</strong> in total.</li>
</ol>
<p>For example, if <code>s = &quot;zbax&quot;</code> and <code>k = 2</code>, then the resulting integer would be <code>8</code> by the following operations:</p>
<ol>
<li><strong>Convert</strong>: <code>&quot;zbax&quot;&quot;(26)(2)(1)(24)&quot;&quot;262124&quot; ➝ 262124</code></li>
<li><strong>Transform #1</strong>: <code>262124 ➝ 2 + 6 + 2 + 1 + 2 + 4 ➝ 17</code></li>
<li><strong>Transform #2</strong>: <code>17 ➝ 1 + 7 ➝ 8</code></li>
</ol>
<p>Return the <strong>resulting</strong> <strong>integer</strong> after performing the <strong>operations</strong> described above.</p>
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;iiii&quot;, k = 1</span></p>
<p><strong>Output:</strong> <span class="example-io">36</span></p>
<p><strong>Explanation:</strong></p>
<p>The operations are as follows:<br />
- Convert: &quot;iiii&quot;&quot;(9)(9)(9)(9)&quot;&quot;9999&quot; ➝ 9999<br />
- Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36<br />
Thus the resulting integer is 36.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;leetcode&quot;, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">6</span></p>
<p><strong>Explanation:</strong></p>
<p>The operations are as follows:<br />
- Convert: &quot;leetcode&quot;&quot;(12)(5)(5)(20)(3)(15)(4)(5)&quot;&quot;12552031545&quot; ➝ 12552031545<br />
- Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33<br />
- Transform #2: 33 ➝ 3 + 3 ➝ 6<br />
Thus the resulting integer is 6.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = &quot;zbax&quot;, k = 2</span></p>
<p><strong>Output:</strong> <span class="example-io">8</span></p>
</div>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= s.length &lt;= 100</code></li>
<li><code>1 &lt;= k &lt;= 10</code></li>
<li><code>s</code> consists of lowercase English letters.</li>
</ul>