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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code