java.lang.Object
g2701_2800.s2745_construct_the_longest_new_string.Solution

public class Solution extends Object
2745 - Construct the Longest New String.<p>Medium</p> <p>You are given three integers <code>x</code>, <code>y</code>, and <code>z</code>.</p> <p>You have <code>x</code> strings equal to <code>&quot;AA&quot;</code>, <code>y</code> strings equal to <code>&quot;BB&quot;</code>, and <code>z</code> strings equal to <code>&quot;AB&quot;</code>. 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 <code>&quot;AAA&quot;</code> or <code>&quot;BBB&quot;</code> as a substring.</p> <p>Return <em>the maximum possible length of the new string</em>.</p> <p>A <strong>substring</strong> is a contiguous <strong>non-empty</strong> sequence of characters within a string.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> x = 2, y = 5, z = 1</p> <p><strong>Output:</strong> 12</p> <p><strong>Explanation:</strong> We can concactenate the strings &ldquo;BB&rdquo;, &ldquo;AA&rdquo;, &ldquo;BB&rdquo;, &ldquo;AA&rdquo;, &ldquo;BB&rdquo;, and &ldquo;AB&rdquo; in that order. Then, our new string is &ldquo;BBAABBAABBAB&rdquo;. That string has length 12, and we can show that it is impossible to construct a string of longer length.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> x = 3, y = 2, z = 2</p> <p><strong>Output:</strong> 14</p> <p><strong>Explanation:</strong> We can concactenate the strings &ldquo;AB&rdquo;, &ldquo;AB&rdquo;, &ldquo;AA&rdquo;, &ldquo;BB&rdquo;, &ldquo;AA&rdquo;, &ldquo;BB&rdquo;, and &ldquo;AA&rdquo; in that order. Then, our new string is &ldquo;ABABAABBAABBAA&rdquo;. That string has length 14, and we can show that it is impossible to construct a string of longer length.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= x, y, z <= 50</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • longestString

      public int longestString(int x, int y, int z)