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.
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.
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.
Strings: s1 = "abcde", s2 = "abcf", s3 = "abxg"
Let's find the longest common prefix:
"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).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.
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 solution where is the length of the shortest string, bypassing any need for actual string modification.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Check Balanced String | Easy | Solve | |
| Find Special Substring of Length K | Easy | Solve | |
| Remove Vowels from a String | Easy | Solve | |
| Check if All A's Appears Before All B's | Easy | Solve | |
| Circular Sentence | Easy | Solve |