Longest Common Prefix After at Most One Removal - Problem
You are given two strings s and t. Return the length of the longest common prefix between s and t after removing at most one character from s.
Note: s can be left without any removal.
Input & Output
Example 1 — Basic Mismatch
$
Input:
s = "abcde", t = "abfgh"
›
Output:
2
💡 Note:
The common prefix without removal is "ab" (length 2). Removing any character from s doesn't improve this, so the answer is 2.
Example 2 — Remove to Extend
$
Input:
s = "axbc", t = "abc"
›
Output:
3
💡 Note:
Without removal: common prefix is "a" (length 1). Removing s[1]='x' gives "abc" vs "abc", common prefix "abc" (length 3).
Example 3 — No Improvement
$
Input:
s = "abc", t = "def"
›
Output:
0
💡 Note:
No common prefix exists regardless of removal. The answer is 0.
Constraints
- 1 ≤ s.length, t.length ≤ 1000
- s and t consist of only lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input
Two strings s and t to compare
2
Process
Find mismatch and try skipping one character from s
3
Output
Length of longest possible common prefix
Key Takeaway
🎯 Key Insight: When characters mismatch, try skipping the problematic character to potentially extend the common prefix
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code