Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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 that 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.
Example 1:
Input: s1 = "abc", s2 = "abb", s3 = "ab"
Output: 2
Explanation: Deleting the rightmost character from both s1 and s2 will result in three equal strings.
Example 2:
Input: s1 = "dac", s2 = "bac", s3 = "cac"
Output: -1
Explanation: Since the first letters of s1 and s2 differ, they cannot be made equal.
Constraints:
1 <= s1.length, s2.length, s3.length <= 100s1, s2 and s3 consist only of lowercase English letters.Problem summary: 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 that 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"abc" "abb" "ab"
"dac" "bac" "cac"
delete-operation-for-two-strings)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2937: Make Three Strings Equal
class Solution {
public int findMinimumOperations(String s1, String s2, String s3) {
int s = s1.length() + s2.length() + s3.length();
int n = Math.min(Math.min(s1.length(), s2.length()), s3.length());
for (int i = 0; i < n; ++i) {
if (!(s1.charAt(i) == s2.charAt(i) && s2.charAt(i) == s3.charAt(i))) {
return i == 0 ? -1 : s - 3 * i;
}
}
return s - 3 * n;
}
}
// Accepted solution for LeetCode #2937: Make Three Strings Equal
func findMinimumOperations(s1 string, s2 string, s3 string) int {
s := len(s1) + len(s2) + len(s3)
n := min(len(s1), len(s2), len(s3))
for i := range s1[:n] {
if !(s1[i] == s2[i] && s2[i] == s3[i]) {
if i == 0 {
return -1
}
return s - 3*i
}
}
return s - 3*n
}
# Accepted solution for LeetCode #2937: Make Three Strings Equal
class Solution:
def findMinimumOperations(self, s1: str, s2: str, s3: str) -> int:
s = len(s1) + len(s2) + len(s3)
n = min(len(s1), len(s2), len(s3))
for i in range(n):
if not s1[i] == s2[i] == s3[i]:
return -1 if i == 0 else s - 3 * i
return s - 3 * n
// Accepted solution for LeetCode #2937: Make Three Strings Equal
fn find_minimum_operations(s1: String, s2: String, s3: String) -> i32 {
let mut s1 = s1;
let mut s2 = s2;
let mut s3 = s3;
let mut ret = 0;
let min_len = std::cmp::min(s1.len(), std::cmp::min(s2.len(), s3.len()));
while s1.len() > min_len {
s1.pop();
ret += 1;
}
while s2.len() > min_len {
s2.pop();
ret += 1;
}
while s3.len() > min_len {
s3.pop();
ret += 1;
}
loop {
if s1 == s2 && s2 == s3 {
return ret;
}
ret += 3;
s1.pop();
s2.pop();
s3.pop();
if s1.is_empty() {
return -1;
}
}
}
fn main() {
let s1 = "abc".to_string();
let s2 = "abb".to_string();
let s3 = "ab".to_string();
let ret = find_minimum_operations(s1, s2, s3);
println!("ret={ret}");
}
#[test]
fn test() {
{
let s1 = "abc".to_string();
let s2 = "abb".to_string();
let s3 = "ab".to_string();
let ret = find_minimum_operations(s1, s2, s3);
assert_eq!(ret, 2);
}
{
let s1 = "dac".to_string();
let s2 = "bac".to_string();
let s3 = "cac".to_string();
let ret = find_minimum_operations(s1, s2, s3);
assert_eq!(ret, -1);
}
{
let s1 = "a".to_string();
let s2 = "a".to_string();
let s3 = "a".to_string();
let ret = find_minimum_operations(s1, s2, s3);
assert_eq!(ret, 0);
}
{
let s1 = "ab".to_string();
let s2 = "ac".to_string();
let s3 = "ad".to_string();
let ret = find_minimum_operations(s1, s2, s3);
assert_eq!(ret, 3);
}
{
let s1 = "abc".to_string();
let s2 = "abc".to_string();
let s3 = "abc".to_string();
let ret = find_minimum_operations(s1, s2, s3);
assert_eq!(ret, 0);
}
}
// Accepted solution for LeetCode #2937: Make Three Strings Equal
function findMinimumOperations(s1: string, s2: string, s3: string): number {
const s = s1.length + s2.length + s3.length;
const n = Math.min(s1.length, s2.length, s3.length);
for (let i = 0; i < n; ++i) {
if (!(s1[i] === s2[i] && s2[i] === s3[i])) {
return i === 0 ? -1 : s - 3 * i;
}
}
return s - 3 * n;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.