← All problems

1304. Longest Happy String

MediumOpen on LeetCodeProblem statement

Problem Statement

1304. Longest Happy String

Medium


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

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.

 

Example 1:

Input: a = 1, b = 1, c = 7
Output: "ccaccbcc"
Explanation: "ccbccacc" would also be a correct answer.

Example 2:

Input: a = 7, b = 1, c = 0
Output: "aabaa"
Explanation: It is the only correct answer in this case.

 

Constraints:

Java — alternative 1

Source file
class Solution {
    public int[] sumZero(int n) {
        int[] ans = new int[n];
        for (int i = n % 2 + 1; i < n; i += 2) {
            ans[i - 1] = i;
            ans[i] = -i;
        }
        return ans;
    }
}