Problem
You are given two strings firstString
and secondString
that are
0-indexed and consist only of lowercase English letters. Count the number of index quadruples (i,j,a,b)
that satisfy the following conditions:
0 <= i <= j < firstString.length
0 <= a <= b < secondString.length
- The substring of
firstString
that starts at theith
character and ends at thejth
character (inclusive) is equal to the substring ofsecondString
that starts at theath
character and ends at thebth
character (inclusive). j - a
is the minimum possible value among all quadruples that satisfy the previous conditions.
Return thenumber of such quadruples.
Examples
Example 1:
|
|
Example 2:
|
|
Constraints:
1 <= firstString.length, secondString.length <= 2 * 10^5
- Both strings consist only of lowercase English letters.
Solution
Method 1 -
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(nnnxxxnnn)
- 🧺 Space complexity:
O(nnnxxx)