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
Visualization
Time & Space Complexity
Single pass through both strings to find mismatch, then at most two additional prefix calculations
Only using a few variables, no additional strings created
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