Skip to content

Latest commit

 

History

History
49 lines (35 loc) · 1.17 KB

_1945. Sum of Digits of String After Convert.md

File metadata and controls

49 lines (35 loc) · 1.17 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : September 03, 2024

Last updated : September 03, 2024


Related Topics : String, Simulation

Acceptance Rate : 74.85 %


Solutions

Python

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)