Sum of Digits of String After Convert

You are given a string s consisting of lowercase English letters, and an integer k. Your task is to convert the string into an integer by a special process, and then transform it by summing its digits repeatedly k times. More specifically, perform the following steps:

  • Convert s into an integer by replacing each letter with its position in the alphabet, i.e. replace 'a' with 1, 'b' with 2, ..., 'z' with 26.
  • Transform the integer by replacing it with the sum of its digits.
  • Repeat the transform operation k times in total.

Return the resulting integer after performing the operations described above.

Example 1
Inputs = "iiii", k = 1
Output36
After converting "iiii" to 9999, one transform gives 9 + 9 + 9 + 9 = 36.
Example 2
Inputs = "leetcode", k = 2
Output6
After converting "leetcode" to 12552031545, the transforms produce 33 and then 6.

Constraints

  • 1 <= s.length <= 100
  • 1 <= k <= 10
  • s consists of lowercase English letters.

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate