[LeetCode] Construct the Longest New String

2745. Construct the Longest New String

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.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
int longestString(int x, int y, int z) {
int mi = min(x,y);
x -= mi, y -= mi;
mi = mi * 2 + z;
mi += (x > 0 or y > 0);
return mi * 2;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/06/25/PS/LeetCode/construct-the-longest-new-string/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.