565. Finding a Needle in the Haystack
0
Easy
Develop a program that can identify all instances of a given pattern within a provided input string. This task is commonly referred to as locating a needle in a haystack.
The program should be able to detect every occurrence of the needle within the haystack. It should accept the needle and the haystack as input, and output the positions of each occurrence, as demonstrated below.
***Note: The positions must be listed in ascending order.***
Input Format
The first line contains the length of the needle.
The second line contains the needle itself.
The third line contains the haystack.
The second line contains the needle itself.
The third line contains the haystack.
Output Format
Print all positions where the needle occurs within the haystack.
Example
Input
9
foobarfoo
barfoobarfoobarfoobarfoobarfoo
Output
3 9 15 21
Constraints
1 <= needle.length <= 10^4
1 <= haystack.length <= 10^5
The string consists of lowercase letters.
1 <= haystack.length <= 10^5
The string consists of lowercase letters.
Loading...
View Submissions
Console