Make Three Strings Equal
Source: leetcode
Difficulty: Easy
Tags: string

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.




C++ solution:
class Solution {
public:
    int findMinimumOperations(string s1, string s2, string s3) {
        int i=0;
        while(i<s1.length() && i<s2.length() && i<s3.length() && s1[i]==s2[i] && s1[i]==s3[i]){
            i++;
        }
        if(i==0) return -1;
        int total=s1.length()+s2.length()+s3.length()-3*i;
        return total;
    }
};

Go solution:
func findMinimumOperations(s1 string, s2 string, s3 string) int {
    i := 0

    for ; i < len(s1) && i < len(s2) && i < len(s3); i++ {
        a, b, c := s1[i], s2[i], s3[i]
        if !(a == b && b == c) { break }
    }

    if i > 0 {return len(s1) + len(s2) + len(s3) - (i * 3)}
    return -1
}

Java solution:
public class Solution {
    public int findMinimumOperations(String s1, String s2, String s3) {
        int len = Math.min(s1.length(), Math.min(s2.length(), s3.length())); // Calculate the length of the shortest string among s1, s2, and s3
        int sum = s1.length() + s2.length() + s3.length(); // Sum of lengths of all three strings

        // Check if the first characters of s1, s2, and s3 are not the same
        if (s1.charAt(0) != s2.charAt(0) || s1.charAt(0) != s3.charAt(0) || s2.charAt(0) != s3.charAt(0)) {
            return -1; // If not the same, return -1 as it's impossible to match the first character
        }

        // Iterate through the strings until a mismatch is found
        for (int i = 0; i < len; i++) {
            if (s1.charAt(i) == s2.charAt(i) && s2.charAt(i) == s3.charAt(i)) {
                sum -= 3; // Decrement the sum by 3 for each matching character in the strings
            } else {
                break; // Break the loop when a mismatch is encountered
            }
        }

        return sum; // Return the sum of lengths of three strings minus the matched characters
    }
}

JavaScript solution:
function findMinimumOperations(s1, s2, s3) {
    let len = Math.min(s1.length, s2.length, s3.length); // Calculate the length of the shortest string among s1, s2, and s3

    let sum = s1.length + s2.length + s3.length; // Sum of lengths of all three strings

    // Check if the first characters of s1, s2, and s3 are not the same
    if (s1[0] !== s2[0] || s1[0] !== s3[0] || s2[0] !== s3[0]) {
        return -1; // If not the same, return -1 as it's impossible to match the first character
    }

    // Iterate through the strings until a mismatch is found
    for (let i = 0; i < len; i++) {
        if (s1[i] === s2[i] && s2[i] === s3[i]) {
            sum -= 3; // Decrement the sum by 3 for each matching character in the strings
        } else {
            break; // Break the loop when a mismatch is encountered
        }
    }

    return sum; // Return the sum of lengths of three strings minus the matched characters
}

Python solution:
class Solution:
    def findMinimumOperations(self, s1: str, s2: str, s3: str) -> int:
        lengths=[len(s1),len(s2),len(s3)]
       
        minimum_length = min(lengths)
        count=0
        for i in range(minimum_length):
            if(s1[i] == s2[i] == s3[i]):
                count+=1
            else:
                break
        if(count==0):
            return -1
        
        result = sum(length-count for length in lengths)
        return result

Ruby solution:
# @param {String} s1
# @param {String} s2
# @param {String} s3
# @return {Integer}
def find_minimum_operations(s1, s2, s3)
    if s1[0] != s2[0] || s2[0] != s3[0]
        return -1
    end

    answer = 0
    while s1 != s2 || s2 != s3
        if s1.size == s2.size and s2.size == s3.size
            s1[-1] = ""
        else
            if s1.size > s2.size || s1.size > s3.size
                s1[-1] = ""
            elsif s2.size > s1.size || s2.size > s3.size
                s2[-1] = ""
            elsif s3.size > s1.size || s3.size > s2.size
                s3[-1] = ""
            end
        end
        answer += 1
    end

    answer
end