Longest Happy String Problem Statement

Given three non-negative integers X, Y, and Z, determine the longest happy string. A happy string is defined as a string that contains only the letters 'a', 'b', and 'c' without having any three consecutive letters being the same.

Explanation:

You need to construct the longest string possible such that it has 'a' at most X times, 'b' at most Y times, and 'c' at most Z times.

If there are multiple strings with maximum length, any of these strings can be returned.

Input:

The first line contains an integer 'T' representing the number of test cases. Each test case consists of one line containing three space-separated integers: X, Y, and Z.

Output:

For each test case, return "1" if a correct happy string can be formed, otherwise return "0".

Example:

Input:
2
1 1 7
2 2 1
Output:
1
1

Constraints:

  • 1 ≤ T ≤ 5
  • 0 ≤ X, Y, Z ≤ 10^3
  • X + Y + Z ≥ 1
Note:
You are not required to input or print anything. Focus on implementing the function to achieve the desired result.
AnswerBot
5mo

The problem involves constructing the longest happy string with given constraints on the frequency of 'a', 'b', and 'c'.

  • Iterate through the characters 'a', 'b', 'c' in decreasing order of their freque...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Interview Questions Asked at Salesforce

Q. Problem Description Given a graph with 'N' nodes and 'M' unidirectional edges, a...read more
Q. Cycle Detection in a Singly Linked List Determine if a given singly linked list ...read more
Q. Design a platform similar to LinkedIn. What are the key features and architectur...read more
Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits