Skip to main content

Command Palette

Search for a command to run...

Sum of Digits of String After Convert

Published
2 min read
Sum of Digits of String After Convert
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

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 by replacing it with the sum of its digits. Repeat the transform operation 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.

LeetCode Problem - 1945

class Solution {
    public int getLucky(String s, int k) {
        // StringBuilder to hold the numeric representation of each character in the string
        StringBuilder sb = new StringBuilder();

        // Convert each character in the input string to its corresponding position in the alphabet
        for (char c : s.toCharArray()) {
            // 'a' corresponds to 1, 'b' to 2, ..., 'z' to 26
            int charPosition = (c - 'a') + 1;
            // Append the numeric value to the StringBuilder
            sb.append(charPosition);
        }

        // Convert the StringBuilder to a string, which now represents the entire numeric transformation of the original string
        String str = sb.toString();
        int answer = 0;  // Variable to hold the result after each transformation step

        // Perform the transformation process k times
        for (int i = 0; i < k; i++) {
            answer = 0;  // Reset the answer for each iteration

            // Sum up the digits of the current string representation
            for (char ch : str.toCharArray()) {
                // Convert the character (which is a digit) to an integer and add it to the answer
                answer += ch - '0';
            }

            // Convert the sum back to a string to be used in the next iteration
            str = String.valueOf(answer);
        }

        // Return the final result after k transformations
        return answer;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.