All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : September 03, 2024
Last updated : September 03, 2024
Related Topics : String, Simulation
Acceptance Rate : 74.85 %
class Solution:
def getLucky(self, s: str, k: int) -> int:
def helper(curr, k: int) -> int :
if not k :
return curr
newCurr = 0
while curr :
newCurr += curr % 10
curr //= 10
return helper(newCurr, k - 1)
newCurr = 0
for c in s :
if ord(c) - ord('a') + 1 >= 10 :
newCurr = 100 * newCurr + ord(c) - ord('a') + 1
else :
newCurr = 10 * newCurr + ord(c) - ord('a') + 1
return helper(newCurr, k)