Problem 698
Substring with Concatenation of All Words
Hard · 30 pts · String, Hash Table, Sliding Window
Problem
You are given a string S and a list of W words, all of the same length L (words may repeat). Find every starting index of a substring of S that is the concatenation of all the words, each used exactly as many times as it appears in the list, in any order and without anything in between. Print the indices in increasing order, or NONE. Slide a window over S in steps of L, once for each starting offset 0..L-1, keeping word counts in a hash map.
Input
The first line contains S. The second line contains W. The third line contains the W words.
Output
Print the starting indices (counted from 0) in increasing order separated by spaces, or NONE.
Constraints
1 <= |S| <= 10000; 1 <= W <= 5000; 1 <= L <= 30; all lowercase letters
Example input
barfoothefoobarman 2 foo bar
Example output
0 9
Explanation
In barfoothefoobarman with the words foo and bar, the substring at index 0 is barfoo and the one at index 9 is foobar. Both are concatenations of the two words, so the answer is 0 9.