Skip to content

2937. Make Three Strings Equal 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
 public:
  int findMinimumOperations(string s1, string s2, string s3) {
    const int minLength = min({s1.length(), s2.length(), s3.length()});
    int i = 0;
    while (i < minLength && s1[i] == s2[i] && s2[i] == s3[i])
      ++i;
    return i == 0 ? -1 : s1.length() + s2.length() + s3.length() - i * 3;
  }
};
1
2
3
4
5
6
7
8
9
class Solution {
  public int findMinimumOperations(String s1, String s2, String s3) {
    final int minLength = Math.min(s1.length(), Math.min(s2.length(), s3.length()));
    int i = 0;
    while (i < minLength && s1.charAt(i) == s2.charAt(i) && s2.charAt(i) == s3.charAt(i))
      ++i;
    return i == 0 ? -1 : s1.length() + s2.length() + s3.length() - i * 3;
  }
}
1
2
3
4
5
6
7
class Solution:
  def findMinimumOperations(self, s1: str, s2: str, s3: str) -> int:
    minLength = min(map(len, [s1, s2, s3]))
    i = 0
    while i < minLength and s1[i] == s2[i] and s2[i] == s3[i]:
      i += 1
    return -1 if i == 0 else len(s1) + len(s2) + len(s3) - i * 3