Skip to main content

Command Palette

Search for a command to run...

Longest Happy String

Published
3 min read
Longest Happy 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.

A string s is called happy if it satisfies the following conditions:

  • s only contains the letters 'a', 'b', and 'c'.

  • s does not contain any of "aaa", "bbb", or "ccc" as a substring.

  • s contains at most a occurrences of the letter 'a'.

  • s contains at most b occurrences of the letter 'b'.

  • s contains at most c occurrences of the letter 'c'.

Given three integers a, b, and c, return the longest possible happy string. If there are multiple longest happy strings, return any of them. If there is no such string, return the empty string "".

A substring is a contiguous sequence of characters within a string.

LeetCode Problem - 1405

class Solution {
    public String longestDiverseString(int a, int b, int c) {
        // Variables to track consecutive 'a', 'b', and 'c' characters added to the result
        int currA = 0, currB = 0, currC = 0;

        // The maximum possible length of the result string will be the total number of characters
        int maxLength = a + b + c;
        int i = 0; // Iterator to control the loop

        StringBuilder sb = new StringBuilder(); // StringBuilder to construct the result string

        // Loop to build the longest possible diverse string
        while (i < maxLength) {
            // Add 'a' if 'a' is not added consecutively twice, and 'a' is greater than or equal to 'b' and 'c', 
            // OR if 'b' or 'c' have already been added twice consecutively
            if (currA != 2 && a >= b && a >= c || a > 0 && (currB == 2 || currC == 2)) {
                sb.append('a'); // Add 'a' to the result
                currA++; // Increment consecutive 'a' count
                currB = 0; // Reset 'b' and 'c' consecutive counts
                currC = 0;
                a--; // Decrement the count of remaining 'a' characters

            // Add 'b' if 'b' is not added consecutively twice, and 'b' is greater than or equal to 'a' and 'c', 
            // OR if 'a' or 'c' have already been added twice consecutively
            } else if (currB != 2 && b >= a && b >= c || b > 0 && (currC == 2 || currA == 2)) {
                sb.append('b'); // Add 'b' to the result
                currB++; // Increment consecutive 'b' count
                currA = 0; // Reset 'a' and 'c' consecutive counts
                currC = 0;
                b--; // Decrement the count of remaining 'b' characters

            // Add 'c' if 'c' is not added consecutively twice, and 'c' is greater than or equal to 'a' and 'b', 
            // OR if 'a' or 'b' have already been added twice consecutively
            } else if (currC != 2 && c >= a && c >= b || c > 0 && (currA == 2 || currB == 2)) {
                sb.append('c'); // Add 'c' to the result
                currC++; // Increment consecutive 'c' count
                currA = 0; // Reset 'a' and 'b' consecutive counts
                currB = 0;
                c--; // Decrement the count of remaining 'c' characters
            }
            i++; // Increment loop iterator
        }

        return sb.toString(); // Return the constructed diverse string
    }
}

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.