
Asked in Barclays
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.

AnswerBot
4mo
Implement a function to count the number of occurrences of a given string within another string in linear time.
Use the Z algorithm to preprocess the pattern string and the concatenated string to effic...read more
Help your peers!
Add answer anonymously...
Interview Questions Asked to Business Technology Analyst at Other Companies
Top Skill-Based Questions for Barclays Business Technology Analyst
SQL Interview Questions and Answers
250 Questions
Data Structures Interview Questions and Answers
250 Questions
C++ Interview Questions and Answers
150 Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

