Sum of Digits of String After Convert

You are given a string `s` of lowercase English letters and an integer `k`. First **convert** `s` into an integer by replacing each letter with its position in the alphabet (`a` with 1, `b` with 2, ..., `z` with 26) and concatenating the results. Then **transform** the integer by replacing it with the sum of its digits. Repeat the transform `k` times in total. For example, `s = "zbax"`, `k = 2`: convert gives `"262124"`, the first transform gives `2+6+2+1+2+4 = 17`, the second gives `1+7 = 8`. Return the resulting integer.

30:00

Sum of Digits of String After Convert
easy
Free written solution
Topics
Companies

You are given a string s of lowercase English letters and an integer k.

First convert s into an integer by replacing each letter with its position in the alphabet (a with 1, b with 2, ..., z with 26) and concatenating the results. Then transform the integer by replacing it with the sum of its digits. Repeat the transform k times in total.

For example, s = "zbax", k = 2: convert gives "262124", the first transform gives 2+6+2+1+2+4 = 17, the second gives 1+7 = 8.

Return the resulting integer.

Example 1:
Input: {"s":"iiii","k":1}
Output: 36
Constraints:
  • 1≤s.length≤1001 \leq s.\text{length} \leq 100

  • 1≤k≤101 \leq k \leq 10

  • s consists of lowercase English letters.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"s":"iiii","k":1}

Write each letter as its alphabet position: "iiii" becomes "9999". Instead of building that long number, add its digits directly, which is transform 1.

convert: letter to alphabet position, digits feed transform 1

i

9

i

9

i

9

i

9

num = 0

further transforms (digit sums)

k = 1: only the conversion pass runs.

value after each transform

T1

Variables
VariableValue
num0
k1
DepthFunction Call
Stack empty
0/6