Loading...
Loading...
Sneha and her friends hide a secret word in their WhatsApp group messages. The letters of the word may appear in any order, but they must be next to each other. For example, cab hides the word abc.
Given the message s and the word p, find every starting position in s where the next p.length letters use exactly the same letters as p, the same number of times each.
Example 1
Input
abacbabc abc
Output
4 2 3 4 6
bac, acb, cba and abc start at positions 2, 3, 4 and 6. aba and bab do not match.
Example 2
Input
hello world
Output
0
No match, so only the count 0 is printed.
Try the problem first. Open one hint at a time.
Two words use the same letters the same number of times exactly when their letter counts match.
Keep an array of 26 counts for p and another for the current p.length letters of s.
Moving one step right, add the new letter to the counts and remove the letter that left. Then compare the two arrays.
If p is longer than s, there is nothing to check: print 0.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor