2745. Construct the Longest New String Medium

@problem@discussion
#Math#Dynamic Programming#Greedy#Brainteaser



1/**
2 * [2745] Construct the Longest New String
3 *
4 * You are given three integers x, y, and z.
5 * 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 concatenate them in some order to form a new string. This new string must not contain "AAA" or "BBB" as a substring.
6 * Return the maximum possible length of the new string.
7 * A substring is a contiguous non-empty sequence of characters within a string.
8 *  
9 * <strong class="example">Example 1:
10 * 
11 * Input: x = 2, y = 5, z = 1
12 * Output: 12
13 * Explanation: We can concatenate the strings "BB", "AA", "BB", "AA", "BB", and "AB" in that order. Then, our new string is "BBAABBAABBAB". 
14 * That string has length 12, and we can show that it is impossible to construct a string of longer length.
15 * 
16 * <strong class="example">Example 2:
17 * 
18 * Input: x = 3, y = 2, z = 2
19 * Output: 14
20 * Explanation: We can concatenate the strings "AB", "AB", "AA", "BB", "AA", "BB", and "AA" in that order. Then, our new string is "ABABAABBAABBAA". 
21 * That string has length 14, and we can show that it is impossible to construct a string of longer length.
22 * 
23 *  
24 * Constraints:
25 * 
26 * 	1 <= x, y, z <= 50
27 * 
28 */
29pub struct Solution {}
30
31// problem: https://leetcode.com/problems/construct-the-longest-new-string/
32// discuss: https://leetcode.com/problems/construct-the-longest-new-string/discuss/?currentPage=1&orderBy=most_votes&query=
33
34// submission codes start here
35
36impl Solution {
37    pub fn longest_string(x: i32, y: i32, z: i32) -> i32 {
38        0
39    }
40}
41
42// submission codes end
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_2745() {
50    }
51}
52


Back
© 2025 bowen.ge All Rights Reserved.