

You have been given two strings “str1” and “str2”. You have to find the length of the longest common substring.
A string “s1” is a substring of another string “s2” if “s2” contains the same characters as in “s1”, in the same order and in continuous fashion also.
For example :If “str1” = “abcjklp” and “str2” = “acjkp” then the output will be 3.
Explanation : The longest common substring is “cjk” which is of length 3.
The first line contains an integer ‘T’ which denotes the number of test cases or queries to be run. Then the test cases are as follows.
The first and only line of each test case contains two space-separated strings str1 and str2, respectively.
Output Format:For each test case, print the length of the longest common substring among the two strings.
Print the output of each test case in a separate line.
1 <= T <= 100
1 <= |str1|, |str2| <= 100
where ‘T’ is the number of test cases and |str| is the length of the string str.
2
abcjklp acjkp
wasdijkl wsdjkl
3
3
2
abcy acby
tyfg cvbnuty
1
2
Explanation: The longest common substrings in first test case are “a”, "b", "c", “y” all having length 1.
Can you think of checking if substrings of the first string also exist in the second string?
The basic idea is to start slicing the first string into the substring and match those substrings in the second string.
O((N*M*min(N,M)), where ‘N’ and ‘M’ are the lengths of the two strings.
Since we are traversing both strings of size ‘N’ and ‘M’ and matching the substrings of both the strings has O(min(N,M)) time complexity. So overall time complexity will be O((N*M*min(N,M)).
O(1).
Constant extra space required.