Longest Common Prefix After at Most One Removal - Problem

Imagine you have two strings s and t, and you want to find the longest common prefix they can share. The twist? You're allowed to remove at most one character from string s to maximize this common prefix length.

A common prefix is a sequence of characters that appears at the beginning of both strings. For example, "abc" is a common prefix of "abcdef" and "abcxyz".

Your task: Return the length of the longest possible common prefix between s and t after removing at most one character from s. Remember, you can also choose to remove no characters at all if that gives the best result!

Example: If s = "abcdef" and t = "acdef", removing the 'b' from s gives us "acdef", which has a common prefix "acdef" with t, resulting in length 5.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "abcdef"\nt = "acdef"
โ€บ Output: 5
๐Ÿ’ก Note: Removing 'b' from s gives "acdef", which has a common prefix "acdef" with t, length 5
example_2.py โ€” No Removal Needed
$ Input: s = "hello"\nt = "help"
โ€บ Output: 3
๐Ÿ’ก Note: Without removal, common prefix is "hel" with length 3. Removing any character doesn't improve this.
example_3.py โ€” Empty Result
$ Input: s = "xyz"\nt = "abc"
โ€บ Output: 0
๐Ÿ’ก Note: No common prefix possible even with removal, so result is 0

Visualization

Tap to expand
๐ŸŽฏ Strategic Character Removal ProcessProblem: Find longest common prefix after removing at most one character from sStep 1: Find Mismatch Points = "abxcd" โ†’ a b [x] c dt = "abycd" โ†’ a b [y] c dMismatch at position 2, current prefix length = 2Step 2: Try RemovalRemove s[2] (x):"ab_cd" vs "abycd"New prefix: "ab" = 2Step 3: AlternativeRemove s[1] (b):"a_xcd" vs "abycd"New prefix: "a" = 1Final ResultMaximum prefix length = 2Optimal: O(n) time, O(1) space๐Ÿ’ก Key insight: Only need to try removing at mismatch position or one before it
Understanding the Visualization
1
Find the Mismatch
Compare characters from left to right until they differ
2
Record Current Progress
Note the common prefix length found so far
3
Try Smart Removals
Remove the mismatched character or the one before it
4
Choose the Best
Return the maximum length among all attempts
Key Takeaway
๐ŸŽฏ Key Insight: Instead of trying all possible removals, we only need to strategically try removing characters at or near the first mismatch point, reducing time complexity from O(nยฒ) to O(n).

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through both strings to find mismatch, then at most two additional prefix calculations

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using a few variables, no additional strings created

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค s.length, t.length โ‰ค 105
  • s and t consist of lowercase English letters only
  • At most one character can be removed from s
Asked in
Google 15 Microsoft 12 Amazon 8 Meta 5
18.0K Views
Medium Frequency
~15 min Avg. Time
450 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