Skip to main content

Command Palette

Search for a command to run...

Reformat The String

Published
2 min read
Reformat The String
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 an alphanumeric string s. (Alphanumeric string is a string consisting of lowercase English letters and digits).

You have to find a permutation of the string where no letter is followed by another letter and no digit is followed by another digit. That is, no two adjacent characters have the same type.

Return the reformatted string or return an empty string if it is impossible to reformat the string.

LeetCode Problem - 1417

class Solution {
    public String reformat(String s) {

        // StringBuilder to store letters and digits separately
        StringBuilder sbLet = new StringBuilder(); // stores letters
        StringBuilder sbDig = new StringBuilder(); // stores digits

        // Loop through each character of the string
        for (char ch : s.toCharArray()) {
            int flag = Integer.valueOf(ch); // get ASCII value of the character

            // Check if the character is a lowercase letter (ASCII range 97-122)
            if (flag > 96 && flag < 123) {
                sbLet.append(ch); // append to letters
            } else {
                sbDig.append(ch); // append to digits
            }
        }

        // Calculate the difference in the number of letters and digits
        int possiblities = sbLet.length() - sbDig.length();
        StringBuilder answer = new StringBuilder(); // to store the final reformatted string

        // Check if it's possible to alternate letters and digits
        if (possiblities == -1 || possiblities == 0 || possiblities == 1) {

            // Case 1: More letters than digits
            if (possiblities == 1) {
                int idxL = 0, idxD = 0; // indexes for letters and digits

                // Loop through the total length of letters + digits
                for (int i = 0; i < sbLet.length() + sbDig.length(); i++) {
                    if (i % 2 == 0) {
                        answer.append(sbLet.charAt(idxL)); // append letter at even positions
                        idxL++;
                    } else {
                        answer.append(sbDig.charAt(idxD)); // append digit at odd positions
                        idxD++;
                    }
                }

            // Case 2: Equal number of letters and digits, or more digits than letters
            } else {
                int idxL = 0, idxD = 0; // indexes for letters and digits

                // Similar loop but starting with a digit at even positions
                for (int i = 0; i < sbLet.length() + sbDig.length(); i++) {
                    if (i % 2 == 0) {
                        answer.append(sbDig.charAt(idxD)); // append digit at even positions
                        idxD++;
                    } else {
                        answer.append(sbLet.charAt(idxL)); // append letter at odd positions
                        idxL++;
                    }
                }                
            }

        } else {
            // If alternating is not possible, return an empty string
            return "";
        }

        // Return the final reformatted string
        return answer.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.