Z Algorithm Problem Statement
You are provided with a string S
of length N
and a string P
of length M
. Your objective is to determine the number of times P
appears in S
in linear time.
Example:
Input:
S = "ababa", P = "ab"
Output:
2
Explanation:
The string "ab" occurs twice in the string "ababa".
Constraints:
- The string consists only of lowercase English alphabets.
1 <= T <= 100
1 <= N, M <= 10^4
- Time Limit: 1 sec
Input:
The first line of input containsT
, the number of test cases.
The first line of each test case contains two integersN
andM
, representing the lengths of stringsS
andP
respectively.
The second line of each test case contains the stringS
.
The third line of each test case contains the stringP
.
Output:
The output for each test case is a single integer indicating the number of occurrences ofP
inS
.
Note:
You do not need to print anything; it has already been handled. Simply implement the function to return the result.
Be the first one to answer
Add answer anonymously...
Popular interview questions of Business Technology Analyst
>
Barclays Business Technology Analyst Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app