Skip to main content

Command Palette

Search for a command to run...

String Compression III

Published
2 min read
String Compression III
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.

Given a string word, compress it using the following algorithm:

  • Begin with an empty string comp. While word is not empty, use the following operation:

    • Remove a maximum length prefix of word made of a single character c repeating at most 9 times.

    • Append the length of the prefix followed by c to comp.

Return the string comp.

LeetCode Problem - 3163

class Solution {
    public String compressedString(String word) {
        // Initialize the count of consecutive characters and the first character to compare
        int count = 1;
        char compChar = word.charAt(0);

        // Use StringBuilder to build the compressed string
        StringBuilder sb = new StringBuilder();

        // Iterate through the input word starting from the second character
        for (int i = 1; i < word.length(); i++) {
            // If the current character is the same as the previous one, increment the count
            if (compChar == word.charAt(i)) {
                count++;

                // If the count reaches 10, append '9' and the character, then reset count to 1
                if (count == 10) {
                    sb.append(9).append(compChar);
                    count = 1; // Reset count after appending 9 and the character
                }
            } else {
                // If the character is different, append the count and the previous character
                sb.append(count).append(compChar);

                // Update the compChar to the new character and reset count
                compChar = word.charAt(i);
                count = 1;
            }
        }

        // Append the final count and character for the last group
        sb.append(count).append(compChar);

        // Return the resulting compressed string
        return sb.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.