Make Three Strings Equal - Problem

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

Input & Output

Example 1 — Basic Case
$ Input: s1 = "abc", s2 = "abcd", s3 = "ab"
Output: 3
💡 Note: The longest common prefix is "ab". Remove 1 character from s1, 2 from s2, and 0 from s3. Total: 1 + 2 + 0 = 3 operations.
Example 2 — No Common Prefix
$ Input: s1 = "abc", s2 = "def", s3 = "ghi"
Output: -1
💡 Note: No common prefix exists, so it's impossible to make them equal.
Example 3 — Already Equal
$ Input: s1 = "test", s2 = "test", s3 = "test"
Output: 0
💡 Note: All strings are already equal, so no operations are needed.

Constraints

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

Visualization

Tap to expand
Make Three Strings Equal by Removing Rightmost CharactersInput:s1 = "abc"s2 = "abcd"s3 = "ab"Find Common PrefixCompare: a==a==a ✓Compare: b==b==b ✓Calculate Operationss1: remove 1 chars2: remove 2 charss3: remove 0 charsCommon Prefix: "ab" → Total Operations: 3abababFinal Result
Understanding the Visualization
1
Input
Three strings of different lengths
2
Find Common Prefix
Identify the longest prefix shared by all strings
3
Calculate Operations
Sum characters to remove from each string
Key Takeaway
🎯 Key Insight: The equal strings must be the longest common prefix of all three input strings
Asked in
Google 15 Amazon 12 Microsoft 8
8.5K Views
Medium Frequency
~10 min Avg. Time
340 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen