LeetCode Entry

1945. Sum of Digits of String After Convert

03.09.2024 easy 2024 kotlin rust

Sum of number chars k times

1945. Sum of Digits of String After Convert easy blog post substack youtube 1.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/723

Problem TLDR

Sum of number chars k times #easy #simulation

Intuition

  • the first transformation is different: c - 'a' + 1
  • other transformations: c - '0'

Approach

  • we can do it with strings or with just numbers

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(1)\)

Code


    fun getLucky(s: String, k: Int) = (1..<k).fold(
        s.map { "${it - 'a' + 1}" }.joinToString("").sumOf { it - '0' }
    ) { r, t -> r.toString().sumOf { it.code - '0'.code }}


    pub fn get_lucky(s: String, k: i32) -> i32 {
        let dig = |x| { let (mut s, mut x) = (0, x);
            while x > 0 { s += x % 10; x /= 10 }; s};
        (1..k).fold(s.bytes().map(|b|
            dig(b as i32 - 96)).sum(), |r, t| dig(r))
    }


    int getLucky(string s, int k) {
        auto dig = [](int x) {
            int s = 0;
            while (x > 0) { s += x % 10; x /= 10; }
            return s;
        };
        int sum = 0;
        for (char c : s) sum += dig(c - 'a' + 1);
        while (k-- > 1) sum = dig(sum);
        return sum;
    }