[LeetCode] Make Three Strings Equal

2937. Make Three Strings Equal

You are given three strings s1, s2, and s3. You have to perform the following operation on these three strings as many times as you want.

In one operation you can choose one of these three strings such that its length is at least 2 and delete the rightmost character of it.

Return the minimum number of operations you need to perform to make the three strings equal if there is a way to make them equal, otherwise, return -1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int findMinimumOperations(string s1, string s2, string s3) {
int res = 0;
while(1) {
if(s1.length() == 0 or s2.length() == 0 or s3.length() == 0) return -1;
if(s1 == s2 and s2 == s3) return res;
int len = max({s1.length(), s2.length(), s3.length()});
if(s1.length() == len) s1.pop_back(), res += 1;
if(s2.length() == len) s2.pop_back(), res += 1;
if(s3.length() == len) s3.pop_back(), res += 1;
if(s1.length() == 0 or s2.length() == 0 or s3.length() == 0) return -1;
}
return -1;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/11/19/PS/LeetCode/make-three-strings-equal/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.