返回介绍

solution / 2900-2999 / 2937.Make Three Strings Equal / README_EN

发布于 2024-06-17 01:02:58 字数 5346 浏览 0 评论 0 收藏 0

2937. Make Three Strings Equal

中文文档

Description

You are given three strings: s1, s2, and s3. In one operation you can choose one of these strings and delete its rightmost character. Note that you cannot completely empty a string.

Return the _minimum number of operations_ required to make the strings equal_. _If it is impossible to make them equal, return -1.

 

Example 1:

Input: s1 = "abc", s2 = "abb", s3 = "ab"

Output: 2

Explanation: Deleting the rightmost character from both s1 and s2 will result in three equal strings.

Example 2:

Input: s1 = "dac", s2 = "bac", s3 = "cac"

Output: -1

Explanation: Since the first letters of s1 and s2 differ, they cannot be made equal.

 

Constraints:

  • 1 <= s1.length, s2.length, s3.length <= 100
  • s1, s2 and s3 consist only of lowercase English letters.

Solutions

Solution 1: Enumeration

According to the problem description, we know that if the three strings are equal after deleting characters, then they have a common prefix of length greater than $1$. Therefore, we can enumerate the position $i$ of the common prefix. If the three characters at the current index $i$ are not all equal, then the length of the common prefix is $i$. At this point, we check if $i$ is $0$. If it is, return $-1$. Otherwise, return $s - 3 \times i$, where $s$ is the sum of the lengths of the three strings.

The time complexity is $O(n)$, where $n$ is the minimum length of the three strings. The space complexity is $O(1)$.

class Solution:
  def findMinimumOperations(self, s1: str, s2: str, s3: str) -> int:
    s = len(s1) + len(s2) + len(s3)
    n = min(len(s1), len(s2), len(s3))
    for i in range(n):
      if not s1[i] == s2[i] == s3[i]:
        return -1 if i == 0 else s - 3 * i
    return s - 3 * n
class Solution {
  public int findMinimumOperations(String s1, String s2, String s3) {
    int s = s1.length() + s2.length() + s3.length();
    int n = Math.min(Math.min(s1.length(), s2.length()), s3.length());
    for (int i = 0; i < n; ++i) {
      if (!(s1.charAt(i) == s2.charAt(i) && s2.charAt(i) == s3.charAt(i))) {
        return i == 0 ? -1 : s - 3 * i;
      }
    }
    return s - 3 * n;
  }
}
class Solution {
public:
  int findMinimumOperations(string s1, string s2, string s3) {
    int s = s1.size() + s2.size() + s3.size();
    int n = min({s1.size(), s2.size(), s3.size()});
    for (int i = 0; i < n; ++i) {
      if (!(s1[i] == s2[i] && s2[i] == s3[i])) {
        return i == 0 ? -1 : s - 3 * i;
      }
    }
    return s - 3 * n;
  }
};
func findMinimumOperations(s1 string, s2 string, s3 string) int {
  s := len(s1) + len(s2) + len(s3)
  n := min(len(s1), len(s2), len(s3))
  for i := range s1[:n] {
    if !(s1[i] == s2[i] && s2[i] == s3[i]) {
      if i == 0 {
        return -1
      }
      return s - 3*i
    }
  }
  return s - 3*n
}
function findMinimumOperations(s1: string, s2: string, s3: string): number {
  const s = s1.length + s2.length + s3.length;
  const n = Math.min(s1.length, s2.length, s3.length);
  for (let i = 0; i < n; ++i) {
    if (!(s1[i] === s2[i] && s2[i] === s3[i])) {
      return i === 0 ? -1 : s - 3 * i;
    }
  }
  return s - 3 * n;
}

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

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

发布评论

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