Count common substrings
Count the pairs (a substring of A, a substring of B) that are equal. Substrings at different positions count separately. For A = aba and B = ab the equal pairs are a–a (twice), b–b and ab–ab, so the answer is 4.
Reported in: Infosys (SP / DSE · 29 Nov 2026) (memory-based)
Input
First line: A. Second line: B.
Output
The number of equal pairs.
Constraints
1 ≤ |A|, |B| ≤ 2000; lowercase letters.
Example 1
Input:
aba
ab
Output:
4Example 2
Input:
aaa
aa
Output:
8Example 3
Input:
abc
xyz
Output:
0Hint: Let L[i][j] be the length of the longest common piece ending at A[i] and B[j]: L[i − 1][j − 1] + 1 when the letters match, else 0. Each (i, j) adds L[i][j] equal pairs, so add them all up.