All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2701_2800.s2745_construct_the_longest_new_string.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g2701_2800.s2745_construct_the_longest_new_string;

// #Medium #Math #Greedy #Brainteaser #2023_09_23_Time_1_ms_(100.00%)_Space_40.1_MB_(66.80%)

/**
 * 2745 - Construct the Longest New String\.
 *
 * Medium
 *
 * You are given three integers `x`, `y`, and `z`.
 *
 * You have `x` strings equal to `"AA"`, `y` strings equal to `"BB"`, and `z` strings equal to `"AB"`. You want to choose some (possibly all or none) of these strings and concactenate them in some order to form a new string. This new string must not contain `"AAA"` or `"BBB"` as a substring.
 *
 * Return _the maximum possible length of the new string_.
 *
 * A **substring** is a contiguous **non-empty** sequence of characters within a string.
 *
 * **Example 1:**
 *
 * **Input:** x = 2, y = 5, z = 1
 *
 * **Output:** 12
 *
 * **Explanation:** We can concactenate the strings "BB", "AA", "BB", "AA", "BB", and "AB" in that order. Then, our new string is "BBAABBAABBAB". That string has length 12, and we can show that it is impossible to construct a string of longer length.
 *
 * **Example 2:**
 *
 * **Input:** x = 3, y = 2, z = 2
 *
 * **Output:** 14
 *
 * **Explanation:** We can concactenate the strings "AB", "AB", "AA", "BB", "AA", "BB", and "AA" in that order. Then, our new string is "ABABAABBAABBAA". That string has length 14, and we can show that it is impossible to construct a string of longer length.
 *
 * **Constraints:**
 *
 * *   `1 <= x, y, z <= 50`
**/
public class Solution {
    public int longestString(int x, int y, int z) {
        int min = Math.min(x, y);
        int res = 0;
        if (x == y) {
            res = 2 * min + z;
        } else {
            res = 2 * min + 1 + z;
        }
        return res * 2;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy