cswork / OAOpen practice workspace

AmazonOA #21

Find Score (p-Matching)

Estimated MediumJudge-ready

Topics (AI-labelled)Two PointersHashing

Given two strings and a positive stride p, count the start indices i such that the m characters at positions i, i+p, …, i+(m−1)p of the first string form a permutation of the second string. All indices must be in range.

Input

The first line contains userID1, the second line userID2, and the third line p. On this site the input consists of lowercase English letters; 1≤m≤n≤1000000, 1≤p≤1000000.

Output

Output a single integer, the answer required by the problem.

Example 1

Input:

acaccaa
aac
2

Output:

2

Example 2

Input:

abc
a
8

Output:

1

Example 3

Input:

ab
ab
2

Output:

0

Example notes

Example 1: the valid starts are 0 and 2 (0-indexed), which take a, a, c at indices 0, 2, 4 and a, c, a at indices 2, 4, 6, both permutations of aac; the other start that can take all three characters, 1, gives c, c, a, which does not match, so the answer is 2. Example 2: the target has only one character, so the stride never produces a second index, and the only start 0 matches a. Example 3: a two-character target needs the start and start+2, which always goes out of range, so the answer is 0.

Editorial

Loading solution…