Loading...
Loading...
A search box highlights every place where a pattern appears in a text. Read the text and the pattern and print how many times the pattern appears, and the starting index of each match (indexes start at 0). Matches may overlap: in aaaa the pattern aa appears at 0, 1 and 2. Capital and small letters are different.
Example 1
Input
banana ana
Output
2 1 3
The two matches share the middle a.
Example 2
Input
the cat sat on the mat at
Output
3 5 9 20
Try the problem first. Open one hint at a time.
indexOf(pattern, from) finds the first match at or after index from, or -1.
After a match at pos, search again from pos + 1 so overlapping matches are found.
Collect the indexes in a StringBuilder and print it only when the count is not 0.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor