1945. Sum of Digits of String After Convert

Tapan RachchhTapan Rachchh
1 min read
class Solution:
    def getLucky(self, s: str, k: int) -> int:
        EXTRA = 96
        letters = []

        def convert(val):
            nonlocal letters            
            letters = []
            strVal = str(val)
            for c in strVal:
                letters.append(int(c))

        init = ""
        for char in s:
            n = ord(char) - EXTRA
            init += str(n)

        convert(init)

        for i in range(k):    
            s = sum(letters)
            convert(s)

        ans = ""
        for e in letters:
            ans += str(e)

        return int(ans)
0
Subscribe to my newsletter

Read articles from Tapan Rachchh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tapan Rachchh
Tapan Rachchh