java.lang.Object
g1901_2000.s1945_sum_of_digits_of_string_after_convert.Solution

public class Solution extends Object
1945 - Sum of Digits of String After Convert.<p>Easy</p> <p>You are given a string <code>s</code> consisting of lowercase English letters, and an integer <code>k</code>.</p> <p>First, <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>, &hellip;, <code>'z'</code> with <code>26</code>). Then, <strong>transform</strong> the integer by replacing it with the <strong>sum of its digits</strong>. Repeat the <strong>transform</strong> operation <code>k</code> <strong>times</strong> in total.</p> <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> <ul> <li><strong>Convert</strong>: <code>&quot;zbax&quot; \u279d &quot;(26)(2)(1)(24)&quot; \u279d &quot;262124&quot; \u279d 262124</code></li> <li><strong>Transform #1</strong>: <code>262124 \u279d 2 + 6 + 2 + 1 + 2 + 4 \u279d 17</code></li> <li><strong>Transform #2</strong>: <code>17 \u279d 1 + 7 \u279d 8</code></li> </ul> <p>Return <em>the resulting integer after performing the operations described above</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;iiii&rdquo;, k = 1</p> <p><strong>Output:</strong> 36</p> <p><strong>Explanation:</strong> The operations are as follows:</p> <ul> <li> <p>Convert: &ldquo;iiii&rdquo; \u279d &ldquo;(9)(9)(9)(9)&rdquo; \u279d &ldquo;9999&rdquo; \u279d 9999</p> </li> <li> <p>Transform #1: 9999 \u279d 9 + 9 + 9 + 9 \u279d 36 Thus the resulting integer is 36.</p> </li> </ul> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;leetcode&rdquo;, k = 2</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong> The operations are as follows:</p> <ul> <li> <p>Convert: &ldquo;leetcode&rdquo; \u279d &ldquo;(12)(5)(5)(20)(3)(15)(4)(5)&rdquo; \u279d &ldquo;12552031545&rdquo; \u279d 12552031545</p> </li> <li> <p>Transform #1: 12552031545 \u279d 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 \u279d 33</p> </li> <li> <p>Transform #2: 33 \u279d 3 + 3 \u279d 6 Thus the resulting integer is 6.</p> </li> </ul> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;zbax&rdquo;, k = 2</p> <p><strong>Output:</strong> 8</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 100</code></li> <li><code>1 <= k <= 10</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • getLucky

      public int getLucky(String s, int k)