LeetCode ASMR: Sum of Digits of String After Convert | Chill Coding

Поделиться
HTML-код
  • Опубликовано: 22 сен 2024
  • I'm starting to feel the incoming fall breeze...

Комментарии • 1

  • @LeekCodeDev
    @LeekCodeDev  20 дней назад +2

    Source Code:
    class Solution:
    def getLucky(self, s: str, k: int) -> int:
    '''
    To solve this problem, I'm first going to convert
    all the characters in the string to its numerical equivalent,
    and call that resulting string "convert". Afterwards, with this
    string that we calculated, run a loop for k number of times,
    and for each iteration, we take the integer version of each character
    of the convert string,add them together, and set that as the new
    convert string, and we repeat this process :D
    '''
    convert = "".join(str(ord(c)-ord('a')+1) for c in list(s))
    res = 0
    for i in range(k):
    res = sum([int(c) for c in convert])
    convert = str(res)
    return int(convert)