LeetCode 1405: Longest Happy String Solution

Master LeetCode problem 1405 (Longest Happy String), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1405. Longest Happy String

Problem Explanation

Explanation

To solve this problem, we can greedily build the longest happy string by considering the most frequent letter at each step while maintaining the constraints on the maximum occurrences of each letter. We can keep track of the remaining counts of each letter and select the letter that has the maximum count without violating the constraints.

  1. Initialize an empty string to store the result.
  2. Create a list of tuples (count, letter) for each letter 'a', 'b', 'c'.
  3. While there are still letters left to add:
    • Sort the list of tuples in descending order of count.
    • Try to add the most frequent letter that doesn't create a substring of length 3 with the same letter.
    • Update the counts and repeat the process until no letter can be added.
  4. Return the resulting string.

The time complexity of this approach is O(a + b + c) as we iterate through the counts of each letter. The space complexity is O(1) as we only use constant space to store the result and the counts of the letters.

Solution Code

class Solution {
    public String longestDiverseString(int a, int b, int c) {
        StringBuilder sb = new StringBuilder();
        PriorityQueue<int[]> pq = new PriorityQueue<>((x, y) -> y[0] - x[0]);
        if (a > 0) pq.offer(new int[]{a, 0});
        if (b > 0) pq.offer(new int[]{b, 1});
        if (c > 0) pq.offer(new int[]{c, 2});
        
        while (!pq.isEmpty()) {
            int[] first = pq.poll();
            if (sb.length() >= 2 && sb.charAt(sb.length() - 1) == first[1] + 'a' && sb.charAt(sb.length() - 2) == first[1] + 'a')
                if (pq.isEmpty()) break;
                else {
                    int[] second = pq.poll();
                    sb.append((char) (second[1] + 'a'));
                    second[0]--;
                    if (second[0] > 0) pq.offer(second);
                    pq.offer(first);
                }
            sb.append((char) (first[1] + 'a'));
            first[0]--;
            if (first[0] > 0) pq.offer(first);
        }
        return sb.toString();
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1405 (Longest Happy String)?

This page provides optimized solutions for LeetCode problem 1405 (Longest Happy String) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1405 (Longest Happy String)?

The time complexity for LeetCode 1405 (Longest Happy String) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1405 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1405 in Java, C++, or Python.

Back to LeetCode Solutions