Magicsheet logo

Equalize Strings by Adding or Removing Characters at Ends

Medium
62.5%
Updated 8/1/2025

Equalize Strings by Adding or Removing Characters at Ends

What is this problem about?

The Equalize Strings by Adding or Removing Characters at Ends coding problem presents two strings and asks for the minimum number of operations to make them identical. However, the operations are restricted: you can only add or remove characters from the ends (prefix or suffix) of the strings. This is essentially asking you to find the longest common substring between the two and then calculate how many characters must be removed to leave only that substring.

Why is this asked in interviews?

Salesforce and other SaaS companies use this to test a candidate's ability to optimize string matching. While it sounds like Edit Distance, the "ends only" restriction makes it a sliding window interview pattern or a hash function interview pattern problem. It evaluates whether you can find the Longest Common Substring (LCS) efficiently.

Algorithmic pattern used

The problem is solved by finding the Longest Common Substring.

  1. Find the length of the longest string S that is a substring of both string1 and string2.
  2. The number of operations is (len1 - len(S)) + (len2 - len(S)).
  3. To find the LCS efficiently:
    • Dynamic Programming: dp[i][j] stores the length of the common suffix of s1[0...i] and s2[0...j]. (O(N*M)).
    • Binary Search + Rolling Hash: Binary search on the length of the common substring and use Rabin-Karp to check for existence. (O((N+M) log N)).

Example explanation

String 1: "abcdef", String 2: "zbcdy"

  1. Common substrings: "bcd" (length 3).
  2. "abcdef" -> "bcd": remove 'a' from front, "ef" from back (3 ops).
  3. "zbcdy" -> "bcd": remove 'z' from front, 'y' from back (2 ops). Total: 5 operations.

Common mistakes candidates make

  • Confusing with Subsequence: Trying to find the Longest Common Subsequence instead of Substring. Since you can only remove from ends, the remaining part must be contiguous in both originals.
  • Complexity: Using a naive O(N^3) search which will time out for long strings.
  • Off-by-one: Errors in DP table indices.

Interview preparation tip

Always distinguish between "substring" (contiguous) and "subsequence" (non-contiguous). The algorithms for each (DP vs. Sliding Window/Hash) are very different.

Similar Questions