返回介绍

solution / 2700-2799 / 2745.Construct the Longest New String / README_EN

发布于 2024-06-17 01:03:00 字数 4417 浏览 0 评论 0 收藏 0

2745. Construct the Longest New String

中文文档

Description

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 concatenate 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

Solutions

Solution 1: Case Discussion

We observe that the string 'AA' can only be followed by 'BB', and the string 'AB' can be placed at the beginning or end of the string. Therefore:

  • If $x < y$, we can first alternately place 'BBAABBAA..BB', placing a total of $x$ 'AA' and $x+1$ 'BB', then place the remaining $z$ 'AB', with a total length of $(x \times 2 + z + 1) \times 2$;
  • If $x > y$, we can first alternately place 'AABBAABB..AA', placing a total of $y$ 'BB' and $y+1$ 'AA', then place the remaining $z$ 'AB', with a total length of $(y \times 2 + z + 1) \times 2$;
  • If $x = y$, we only need to alternately place 'AABB', placing a total of $x$ 'AA' and $y$ 'BB', then place the remaining $z$ 'AB', with a total length of $(x + y + z) \times 2$.

The time complexity is $O(1)$, and the space complexity is $O(1)$.

class Solution:
  def longestString(self, x: int, y: int, z: int) -> int:
    if x < y:
      return (x * 2 + z + 1) * 2
    if x > y:
      return (y * 2 + z + 1) * 2
    return (x + y + z) * 2
class Solution {
  public int longestString(int x, int y, int z) {
    if (x < y) {
      return (x * 2 + z + 1) * 2;
    }
    if (x > y) {
      return (y * 2 + z + 1) * 2;
    }
    return (x + y + z) * 2;
  }
}
class Solution {
public:
  int longestString(int x, int y, int z) {
    if (x < y) {
      return (x * 2 + z + 1) * 2;
    }
    if (x > y) {
      return (y * 2 + z + 1) * 2;
    }
    return (x + y + z) * 2;
  }
};
func longestString(x int, y int, z int) int {
  if x < y {
    return (x*2 + z + 1) * 2
  }
  if x > y {
    return (y*2 + z + 1) * 2
  }
  return (x + y + z) * 2
}
function longestString(x: number, y: number, z: number): number {
  if (x < y) {
    return (x * 2 + z + 1) * 2;
  }
  if (x > y) {
    return (y * 2 + z + 1) * 2;
  }
  return (x + y + z) * 2;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文