Count Pairs of Equal Substrings With Minimum Difference - 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.length0 <= a <= b < secondString.length- The substring of
firstStringthat starts at theith character and ends at thejth character (inclusive) is equal to the substring ofsecondStringthat starts at theath character and ends at thebth character (inclusive) j - ais the minimum possible value among all quadruples that satisfy the previous conditions
Return the number of such quadruples.
Input & Output
Example 1 — Basic Case
$
Input:
firstString = "abc", secondString = "bc"
›
Output:
2
💡 Note:
Matching substrings: "b" at (1,1) and (0,0) with j-a=1, "c" at (2,2) and (1,1) with j-a=1. Both achieve minimum j-a=1, so count is 2.
Example 2 — Single Character
$
Input:
firstString = "a", secondString = "a"
›
Output:
1
💡 Note:
Only one match: "a" at (0,0) and (0,0) with j-a=0. This is the minimum, so count is 1.
Example 3 — No Matches
$
Input:
firstString = "abc", secondString = "def"
›
Output:
0
💡 Note:
No common substrings between the two strings, so no valid quadruples exist.
Constraints
- 1 ≤ firstString.length, secondString.length ≤ 100
- firstString and secondString consist only of lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input Strings
firstString = 'abc', secondString = 'bc'
2
Find Matches
Compare all substring pairs and calculate j-a differences
3
Count Minimum
Count pairs with minimum j-a value
Key Takeaway
🎯 Key Insight: Group equal substrings and find the minimum j-a difference among all matching pairs
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code