Magicsheet logo

Make Three Strings Equal

Easy
25%
Updated 8/1/2025

Asked by 1 Company

Topics

Make Three Strings Equal

What is this problem about?

The Make Three Strings Equal problem provides three strings. You are allowed to perform an operation where you delete the rightmost character of any of the three strings. Your goal is to find the minimum number of operations required to make all three strings perfectly identical. If it is impossible to make them identical (other than making them all empty), you should return -1.

Why is this asked in interviews?

This is a straightforward string manipulation problem that tests basic iteration and edge case handling. Interviewers use it as an early screening question to see if a candidate can identify the mathematical shortcut. It evaluates whether you recognize that removing from the right means the final identical string must be a common prefix shared by all three original strings.

Algorithmic pattern used

This problem relies on a Common Prefix Search pattern. Because you can only delete from the right end, the longest string that can possibly remain is the longest common prefix shared by all three strings.

  1. Find the length of the longest common prefix of the three strings.
  2. If the length is 0 (they don't even share the first character), return -1.
  3. Otherwise, the minimum operations required is the total length of all three strings minus three times the length of their common prefix.

Example explanation

Strings: s1 = "abcde", s2 = "abcf", s3 = "abxg" Let's find the longest common prefix:

  • Index 0: 'a', 'a', 'a'. Match.
  • Index 1: 'b', 'b', 'b'. Match.
  • Index 2: 'c', 'c', 'x'. Mismatch! The longest common prefix is "ab", which has a length of 2. Total characters initially = 5 + 4 + 4 = 13. Characters we want to keep = 3 strings * length 2 = 6. Operations needed = 13 - 6 = 7. (We delete "cde" from s1, "cf" from s2, "xg" from s3).

Common mistakes candidates make

A common error is overcomplicating the prefix search by using expensive substring methods or nested loops. Since you just need the length of the common prefix, a single while loop tracking a single index i is the cleanest and fastest approach. Another mistake is forgetting the edge case where the strings have no common prefix at all; returning the total length instead of -1 will fail tests.

Interview preparation tip

For the Make Three Strings Equal coding problem, immediately translate the physical action ("deleting from the right") into its structural equivalent ("finding the common prefix"). Recognizing this translation allows you to jump straight into an O(N)O(N) solution where NN is the length of the shortest string, bypassing any need for actual string modification.

Similar Questions