algoadvance

Leetcode 1945. Sum of Digits of String After Convert

Problem Statement

You are given a string s consisting of lowercase English letters and an integer k. First, convert s into an integer by replacing each letter with its position in the alphabet (i.e., convert ‘a’ to 1, ‘b’ to 2, …, ‘z’ to 26). Then, transform the resulting integer into a sum of its digits until you have performed the operation k times. Return the resulting integer after performing the k operations.

Clarifying Questions

  1. Input Constraints:
    • Length of s (1 <= s.length <= 100).
    • 1 <= k <= 10.
  2. Output:
    • Return the integer result from the described operations.
  3. Examples to clarify the problem:
    • Example 1:
      Input: s = "iiii", k = 1
      Output: 36
      

      Explanation:

      • ‘i’ -> 9
      • iiii -> “9999” -> Summing digits -> 9 + 9 + 9 + 9 = 36
    • Example 2:
      Input: s = "leetcode", k = 2
      Output: 6
      

      Explanation:

      • l = 12, e = 5, e = 5, t = 20, c = 3, o = 15, d = 4, e = 5
      • “leetcode” -> “12552031545” -> Sum digits: 1 + 2 + 5 + 5 + 2 + 0 + 3 + 1 + 5 + 4 + 5 = 33
      • Perform again: Sum digits of 33 -> 3 + 3 = 6

Strategy

  1. String Conversion:
    • Convert each letter in the string to its corresponding number.
  2. Sum of Digits Calculation:
    • Convert the resulting number from the above step into a sum of its digits.
    • Repeat the sum of digits operation k times.

Code

public class Solution {
    public int getLucky(String s, int k) {
        // Step 1: Convert each character to its corresponding number in string form
        StringBuilder numStr = new StringBuilder();
        for (char c : s.toCharArray()) {
            int number = c - 'a' + 1;
            numStr.append(number);
        }

        // Step 2: Convert the concatenated string of numbers into an integer and sum its digits
        int result = sumOfDigits(numStr.toString());

        // Step 3: Repeat the sum of digits operation k-1 more times
        for (int i = 1; i < k; i++) {
            result = sumOfDigits(Integer.toString(result));
        }

        return result;
    }

    private int sumOfDigits(String num) {
        int sum = 0;
        for (char c : num.toCharArray()) {
            sum += c - '0';
        }
        return sum;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.getLucky("iiii", 1)); // Output: 36
        System.out.println(solution.getLucky("leetcode", 2)); // Output: 6
    }
}

Time Complexity

  1. String Conversion:
    • O(n), where n is the length of the string s.
  2. Sum of Digits Calculation:
    • Calculating sum of digits of a number with m digits takes O(m) time.
    • Summing digits of a number in repeated k iterations: O(k * m).

Since m (the number of digits) grows depending on the transformed integers, but given the constraints, this method operates efficiently.

Cut your prep time in half and DOMINATE your interview with AlgoAdvance AI