
‘S’ = “aba” ‘T’ = “aca” then
(a, c)
(b, a)
(b, c)
(b, a)
(a, c)
(ab, ac)
(ba, ca)
(aba, aca)
Total 8 strings.
The first line of input contains an integer 'T' representing the number of test cases.
The next two lines of each test case contains two strings ‘S’ and ‘T’.
For each test case, print a single line containing a single integer denoting the number of described substrings.
The output for each test case is printed in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N, M <= 200
Where ‘T’ is the number of test cases, ‘N’ and ‘M’ is the length of the strings ‘S’ and ‘T’ respectively.
Time limit: 1 sec.
Here, the idea is to compare all substrings and add only those substrings resulting in only one mismatched character.
We can achieve this by storing the number of mismatched characters in each substring.
Algorithm:
The idea here is to optimize the previous approach using dynamic programming. As we are calculating the l and r value for every mismatched character again and again so we will precompute sizes of a matching substring ending at position ( i, j ) and starting at position
( i, j )
DPL[ i ][ j ] : size of matching substring ending at position (i, j)
DPR[ i ][ j ] : size of matching substring starting at position (i, j)
Algorithm: