Make Three Strings Equal

Medium
1086.

Make Three Strings Equal

Make Three Strings Equal is a medium coding problem on strings, asked in Amazon, Google and Salesforce interviews.

In one operation you pick one of the three strings — it must have length at least 2 — and delete its rightmost character.

Return the minimum number of operations that makes all three strings equal, or -1 if that is impossible.

Example 1

Input
s1 = "abc", s2 = "abb", s3 = "ab"
Output
2
Explanation
Trim `abc` and `abb` down to `ab`.

Example 2

Input
s1 = "dac", s2 = "bac", s3 = "cac"
Output
-1
Explanation
The first characters already differ, and only the right end can be trimmed.

Example 3

Input
s1 = "aab", s2 = "aa", s3 = "aaa"
Output
2
Explanation
All three trim to `aa`.

Constraints

  • 1 <= s1.length, s2.length, s3.length <= 100
  • s1, s2 and s3 consist only of lowercase English letters.
Write your solution here. Running it against the tests needs a free account — what you type comes with you.Sign in to run
/**
 * @param {string} s1
 * @param {string} s2
 * @param {string} s3
 * @return {number}
 */
var findMinimumOperations = function(s1, s2, s3) {
    // Write your code here
    
};

Sign in to run your own test cases

Sign in

Make Three Strings Equal — Solution in Python, Java & C++

Make Three Strings Equal solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem on strings.

Make Three Strings Equal is a medium coding problem on strings, asked in Amazon, Google and Salesforce interviews.

  • Difficulty: Medium
  • Topics: Strings
  • Asked at: Amazon, Google, Salesforce
  • Time limit: 2 s
  • Memory limit: 256 MB
  • Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby

Problem statement

In one operation you pick one of the three strings — it must have length at least 2 — and delete its rightmost character.

Return the minimum number of operations that makes all three strings equal, or -1 if that is impossible.

Example 1

Input: s1 = "abc", s2 = "abb", s3 = "ab"
Output: 2
Explanation: Trim `abc` and `abb` down to `ab`.

Example 2

Input: s1 = "dac", s2 = "bac", s3 = "cac"
Output: -1
Explanation: The first characters already differ, and only the right end can be trimmed.

Example 3

Input: s1 = "aab", s2 = "aa", s3 = "aaa"
Output: 2
Explanation: All three trim to `aa`.

Constraints

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

Hints for Make Three Strings Equal

  1. Hint 1

    Deleting from the right means every string can only become one of its own prefixes.

  2. Hint 2

    So the final common string is the longest common prefix of all three.

  3. Hint 3

    If that prefix is empty the answer is -1.

How to solve Make Three Strings Equal

Only right-hand deletions are allowed, so each string can only shrink to one of its prefixes. The three can therefore meet only at a common prefix, and the cheapest meeting point is the longest one. The cost is the total length minus three times that prefix's length.

Approach

  1. Walk the three strings in lockstep while all three characters agree.
  2. Return -1 if not even the first characters match.
  3. Otherwise return len1 + len2 + len3 - 3 · common.

Why it works

Longest is cheapest because every extra shared character saves three deletions — one per string — so there is no trade-off to weigh. The -1 case is exactly an empty common prefix, since a string of length 1 cannot be trimmed further and an empty target is unreachable.

Complexity

  • Time — O(min length)
  • Space — O(1)

Pitfalls

  • The common prefix matters, not the common subsequence or suffix.
  • The operation requires length ≥ 2, so no string can be emptied — an empty common prefix means -1.
  • The cost counts deletions across all three strings.

Reference solution

Python

def findMinimumOperations(s1: str, s2: str, s3: str) -> int:
    limit = min(len(s1), len(s2), len(s3))
    common = 0
    while common < limit and s1[common] == s2[common] == s3[common]:
        common += 1
    if common == 0:
        return -1
    return len(s1) + len(s2) + len(s3) - 3 * common

Java

public static int findMinimumOperations(String s1, String s2, String s3) {
    int limit = Math.min(s1.length(), Math.min(s2.length(), s3.length()));
    int common = 0;
    while (common < limit
            && s1.charAt(common) == s2.charAt(common)
            && s2.charAt(common) == s3.charAt(common)) common++;
    if (common == 0) return -1;
    return s1.length() + s2.length() + s3.length() - 3 * common;
}

C++

int findMinimumOperations(string s1, string s2, string s3) {
    size_t limit = min(s1.size(), min(s2.size(), s3.size()));
    size_t common = 0;
    while (common < limit && s1[common] == s2[common] && s2[common] == s3[common]) common++;
    if (common == 0) return -1;
    return (int) (s1.size() + s2.size() + s3.size() - 3 * common);
}

JavaScript

var findMinimumOperations = function(s1, s2, s3) {
    var limit = Math.min(s1.length, Math.min(s2.length, s3.length));
    var common = 0;
    while (common < limit
        && s1.charAt(common) === s2.charAt(common)
        && s2.charAt(common) === s3.charAt(common)) common++;
    if (common === 0) return -1;
    return s1.length + s2.length + s3.length - 3 * common;
};

Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 515 strings problems · the whole catalogue

Learn the technique: Strings