An old mechanical cipher wheel encodes a lowercase passphrase s by replacing every letter with its 1-indexed position in the alphabet ('a' -> 1, 'b' -> 2, ..., 'z' -> 26) and concatenating those numbers, in order, into one (potentially very long) decimal integer. The wheel then "digests" this integer k times: each digest step replaces the current integer with the sum of its decimal digits.
Given s and k, report the integer that results after the wheel's initial encoding followed by exactly k digest steps.
s.k.A single integer: the value after encoding s and then applying the digest step k times.
s consists only of lowercase English letters ('a'-'z').Example 1
Input
iiii 1
Expected
36
Explanation
The letter 'i' is the 9th letter, so each of the four i's becomes "9", concatenating to "9999". One digest step sums its digits: 9+9+9+9 = 36.
Example 2
Input
leetcode 2
Expected
6
Explanation
Encoding "leetcode" letter by letter (l=12, e=5, e=5, t=20, c=3, o=15, d=4, e=5) and concatenating gives the number 12552031545. The first digest step sums its digits (1+2+5+5+2+0+3+1+5+4+5) to get 33. The second digest step sums the digits of 33 (3+3) to get 6.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →