1945. Sum of Digits of String After Convert LeetCode Solution

In this guide, you will get 1945. Sum of Digits of String After Convert LeetCode Solution with the best time and space complexity. The solution to Sum of Digits of String After Convert problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Sum of Digits of String After Convert solution in C++
  4. Sum of Digits of String After Convert solution in Java
  5. Sum of Digits of String After Convert solution in Python
  6. Additional Resources
1945. Sum of Digits of String After Convert LeetCode Solution image

Problem Statement of Sum of Digits of String After Convert

You are given a string s consisting of lowercase English letters, and an integer k. Your task is to convert the string into an integer by a special process, and then transform it by summing its digits repeatedly k times. More specifically, perform the following steps:

Convert s into an integer by replacing each letter with its position in the alphabet (i.e. replace ‘a’ with 1, ‘b’ with 2, …, ‘z’ with 26).
Transform the integer by replacing it with the sum of its digits.
Repeat the transform operation (step 2) k times in total.

For example, if s = “zbax” and k = 2, then the resulting integer would be 8 by the following operations:

Convert: “zbax” ➝ “(26)(2)(1)(24)” ➝ “262124” ➝ 262124
Transform #1: 262124 ➝ 2 + 6 + 2 + 1 + 2 + 4 ➝ 17
Transform #2: 17 ➝ 1 + 7 ➝ 8

Return the resulting integer after performing the operations described above.

See also  3295. Report Spam Message LeetCode Solution

Example 1:

Input: s = “iiii”, k = 1
Output: 36
Explanation:
The operations are as follows:
– Convert: “iiii” ➝ “(9)(9)(9)(9)” ➝ “9999” ➝ 9999
– Transform #1: 9999 ➝ 9 + 9 + 9 + 9 ➝ 36
Thus the resulting integer is 36.

Example 2:

Input: s = “leetcode”, k = 2
Output: 6
Explanation:
The operations are as follows:
– Convert: “leetcode” ➝ “(12)(5)(5)(20)(3)(15)(4)(5)” ➝ “12552031545” ➝ 12552031545
– Transform #1: 12552031545 ➝ 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 ➝ 33
– Transform #2: 33 ➝ 3 + 3 ➝ 6
Thus the resulting integer is 6.

Example 3:

Input: s = “zbax”, k = 2
Output: 8

Constraints:

1 <= s.length <= 100
1 <= k <= 10
s consists of lowercase English letters.

Complexity Analysis

  • Time Complexity:
  • Space Complexity:

1945. Sum of Digits of String After Convert LeetCode Solution in C++

class Solution {
 public:
  int getLucky(string s, int k) {
    int ans = convert(s);
    for (int i = 1; i < k; ++i)
      ans = getDigitSum(ans);
    return ans;
  }

 private:
  int convert(string s) {
    int sum = 0;
    for (const char c : s) {
      const int val = c - 'a' + 1;
      // Do one transform to prevent integer overflow.
      sum += val < 10 ? val : (val % 10 + val / 10);
    }
    return sum;
  }

  int getDigitSum(int num) {
    int digitSum = 0;
    while (num > 0) {
      digitSum += num % 10;
      num /= 10;
    }
    return digitSum;
  }
};
/* code provided by PROGIEZ */

1945. Sum of Digits of String After Convert LeetCode Solution in Java

class Solution {
  public int getLucky(String s, int k) {
    int ans = convert(s);
    for (int i = 1; i < k; ++i)
      ans = getDigitSum(ans);
    return ans;
  }

  private int convert(final String s) {
    int sum = 0;
    for (final char c : s.toCharArray()) {
      final int val = c - 'a' + 1;
      // Do one transform to prevent integer overflow.
      sum += val < 10 ? val : (val % 10 + val / 10);
    }
    return sum;
  }

  private int getDigitSum(int num) {
    int digitSum = 0;
    while (num > 0) {
      digitSum += num % 10;
      num /= 10;
    }
    return digitSum;
  }
}
// code provided by PROGIEZ

1945. Sum of Digits of String After Convert LeetCode Solution in Python

class Solution:
  def getLucky(self, s: str, k: int) -> int:
    ans = self._convert(s)
    for _ in range(k):
      ans = self._getDigitSum(ans)
    return ans

  def _convert(self, s: str) -> int:
    return int(''.join(str(ord(c) - ord('a') + 1) for c in s))

  def _getDigitSum(self, num: int) -> int:
    return sum(int(digit) for digit in str(num))
# code by PROGIEZ

Additional Resources

See also  1238. Circular Permutation in Binary Representation LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.