Skip to main content

Command Palette

Search for a command to run...

Decrypt String from Alphabet to Integer Mapping

Published
2 min read
Decrypt String from Alphabet to Integer Mapping
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 formed by digits and '#'. We want to map s to English lowercase characters as follows:

  • Characters ('a' to 'i') are represented by ('1' to '9') respectively.

  • Characters ('j' to 'z') are represented by ('10#' to '26#') respectively.

Return the string formed after mapping.

The test cases are generated so that a unique mapping will always exist.

LeetCode Problem - 1309

class Solution {
    // Method to decode a string 's' into its corresponding alphabet characters
    public String freqAlphabets(String s) {
        StringBuilder sb = new StringBuilder();  // StringBuilder to store decoded characters

        // Iterate through the string 's' from the end to the beginning
        for (int i = s.length() - 1; i >= 0; i--) {
            int k = 0;  // Variable to store numeric value

            // Check if current character is '#'
            if (s.charAt(i) == '#') {
                // Extract the numeric value from the substring (last two characters before '#')
                k = Integer.parseInt(s.substring(i - 2, i));
                i -= 2;  // Move index back by 2 to skip the processed substring
            } else {
                // Convert the current character to its integer value
                char ch = s.charAt(i);
                int integerValue = ch - '0';  // Convert character to integer value
                k = integerValue;  // Assign integer value to 'k'
            }

            k += 96;  // Adjust 'k' to ASCII value of corresponding alphabet characters (a = 97)
            sb.append((char) k);  // Append decoded character to StringBuilder
        }

        // Reverse the StringBuilder to get the decoded string in correct order
        return sb.reverse().toString();
    }
}

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.