Game of Matchings
Time limit2sMemory limit512 MB
Count substrings of S that match pattern P under a bijective letter-to-number mapping, where distinct numbers must get distinct letters.
- Level
Hard8 of 10
- Topics
- String matching, String, Hash map, Sliding window
- Solved
- No attempts yet
Problem
Adam and Carol are playing the game of matchings. The board is a string of lowercase English letters, and the goal is to find every place where a special kind of pattern matches inside . The pattern has length and is given by integers between 1 and 26.
The contiguous substring starting at position is a matching of when some mapping from the numbers of to lowercase letters meets both conditions: the mapping turns the pattern into , and no two distinct numbers map to the same letter.
For example, if is awawww and is , the matchings are the two substrings of length three that start at positions 1 and 2, awa and waw. The substring www is not a matching, because the numbers 10 and 21 would both have to map to the letter w.
Adam and Carol lost the answer sheet and are not sure whether they found every matching. Given and , count the matchings.
Input
The first line contains the string . It is not empty, its length is at most , and every character is a lowercase letter from a to z.
The second line contains the pattern length ().
The third line contains the integers of the pattern, separated by spaces ().
Output
Print one integer, the number of matchings of found in .