Similar Problems

Similar Problems not available

Sum Of Digits Of String After Convert - Leetcode Solution

Companies:

LeetCode:  Sum Of Digits Of String After Convert Leetcode Solution

Difficulty: Easy

Topics: string simulation  

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., replace 'a' with 1, 'b' with 2, ..., 'z' with 26). Then, transform the integer into its sum of digits. Repeat the transform k times in total.

For example, if s = "zbax" and k = 2, then the resulting integer would be 8 by converting "zbax" into 2621184, and then transforming 2621184 into 2 + 6 + 2 + 1 + 1 + 8 + 4 = 24, and then transforming 24 into 2 + 4 = 6.

Return the resulting integer after performing the operations described above.

Solution:

Our approach to this problem would be a simple one. We will iterate through each character of the string, get its alphabetical position using ASCII code, sum up the positions together and then convert the sum into its sum of digits as described earlier.

Let's go through the steps one by one:

Step 1: Convert the string s into an integer sum using ASCII code. Iterate through each character of the string s, get its ASCII code and subtract the ASCII code of 'a' from it. Then add 1 to get its alphabetical position. We will store the sum of the alphabetical positions in the variable sum.

    int sum = 0;
    for(char c: s.toCharArray()){
        sum += (c-'a'+1);
    }

Step 2: Transform the integer sum into its sum of digits. We will keep on dividing the sum by 10 until it becomes 0. At each iteration, we will take the remainder of the division and add it to the variable digitSum.

    int digitSum = 0;
    while(sum>0){
        digitSum += sum%10;
        sum /= 10;
    }

Step 3: Repeat the above steps k times. We will simply put the above two steps in a loop that runs k times.

    int sum = 0;
    for(char c: s.toCharArray()){
        sum += (c-'a'+1);
    }
    
    int digitSum = 0;
    for(int i=0; i<k; i++){
        while(sum>0){
            digitSum += sum%10;
            sum /= 10;
        }
        sum = digitSum;
        digitSum = 0;
    }

Step 4: Return the final digitSum.

    return digitSum;

Putting all these steps together, we get the following Java code:

class Solution { public int getLucky(String s, int k) { int sum = 0; for(char c: s.toCharArray()){ sum += (c-'a'+1); }

    int digitSum = 0;
    for(int i=0; i<k; i++){
        while(sum>0){
            digitSum += sum%10;
            sum /= 10;
        }
        sum = digitSum;
        digitSum = 0;
    }
    
    return digitSum;
}

}

Time Complexity: The time complexity of the above algorithm is O(n*k) where n is the length of the string s.

Space Complexity: The space complexity of the above algorithm is O(1). We are not using any extra space apart from a few integer variables.

This solution passes all the test cases and is an optimal one in terms of time and space complexity.

Sum Of Digits Of String After Convert Solution Code

1