This time Ninja is fighting with string man, but string man is too powerful for the Ninja to defeat him alone. So Ninja needs your help to defeat him, Ninja has two strings βSβ and βTβ to defeat string man Ninja has to find the number of substrings in βSβ that differ from some substrings of βTβ by exactly one character.
Help Ninja by telling him the number of such types of substrings.
For example:
βSβ = βabaβ βTβ = βacaβ then
(a, c)
(b, a)
(b, c)
(b, a)
(a, c)
(ab, ac)
(ba, ca)
(aba, aca)
Total 8 strings.
Input Format:
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β.
Output Format :
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.
Note:
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.
2
ab
ac
aa
bb
4
4
Test Case 1 : Four cases are described below:
( a, c )
( b, a )
( b, c )
( ab, ac )
Test Case 2: Four cases are described below:
( a , b )
( a , b )
( a , b )
( a , b )
2
codingninjasisawesome
codingninjasisawesome
aabbcddee
aabbeddee
444
106
Try to calculate number of substring using two pointers.
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:
O( N * M * min(N, M) ), where βNβ and βMβ is the length of the given strings βSβ and βTβ.
We are running three nested loops till strings length.
O( 1 ).
We are storing nothing so space complexity is O(1).