AlgoUniversity
  • Go Back
Discussion
Smallest Common Substring :

Author

Ayush Gangwani

Difficulty Level : Easy

Submissions : 281

Asked In : Sprinklr

Marks :10

: 1 | : 0

You are given two strings $$$s$$$ and $$$t$$$ consisting of lowercase Latin letters. Find the number of different pairs $$$((i,j), \hspace{1pt} (k,l))$$$ such that the substrings $$$s[i:j]$$$ and $$$t[k:l]$$$ are equal and the value of $$$j-i+1$$$ is minimum.

Input

The first line of input contains an integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of testcases. The description of $$$t$$$ testcases follows.

The first line of each testcase contains two space separated integers $$$n$$$ and $$$m$$$ $$$(1 \le n, m \le 10^5)$$$ — the lengths of strings $$$s$$$ and $$$t$$$ respectively.

The second line of each testcase contains the string $$$s$$$ containing $$$n$$$ lowercase latin letters.

The third line of each testcase contains the string $$$t$$$ containing $$$m$$$ lowercase latin letters.

It is guaranteed that the sum of $$$n$$$ and $$$m$$$ over all testcases does not exceed $$$10^5$$$.

Output

For each testcase, print the answer to the problem in a single line.

Example

Input
1
4 2
abdc
bd
Output
2

Note

In sample test case 1, Consider $$$s$$$ = "abdc" and $$$t$$$ = "bd". You must determine the answer such that it meets the given conditions:

  • Let us choose a substring of length $$$1$$$, those are "b" and "d". The count is $$$2$$$.
  • Let us choose a substring of length $$$2$$$, which is "bd". The count is $$$1$$$.
Since we need to minimize the length of matching substring hence the answer to the corresponding example is $$$2$$$.

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.