[LeetCode] Longest Common Prefix After at Most One Removal

3460. Longest Common Prefix After at Most One Removal

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.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
int longestCommonPrefix(string s, string t) {
int i = 0, j = 0, res = 0;
while(i < s.length() and j < t.length() and abs(i-j) <= 1) {
if(s[i] == t[j]) i++,j++,res++;
else i++;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/11/22/PS/LeetCode/longest-common-prefix-after-at-most-one-removal/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.