Make Three Strings Equal — Medium Problem & Solution

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

  • 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.

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

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#, C++, Go, Java, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.

All 282 strings problems · the whole catalogue