
1. A string ‘S’ is said to be ‘K’ periodic, if for each position ‘i’ in the string S[i] = S[i+K].
Consider string ‘S’,
if S = ”abcabc”, it is 3 periodic string.
if S= ”aaaaa”, it is 1 periodic string.
2. In one move, only one character of ‘S’ can be swapped with a character of ‘A’.
3. The characters in ‘A’ can be used more than once.
4. All characters of K-periodic string ‘S’ are elements of array ‘A’.
First-line will contain an integer ‘T’, the number of test cases. Then the test cases follow-
Each test case contains three lines of input.
The first line contains three integers ‘N’, ‘M’, ‘K’. The second line contains a string of length ‘N’.
The third line contains ‘M’ space-separated smaller case letters.
For each test case output an integer i.e minimum number of swaps required to make string K-periodic.
1 <= T <= 5
1 <= N, M <= 2 * 10 ^ 4
1 <= K < = N
Time limit: 1 sec.
Use a 2-dimensional array ‘frequency[K][26]’, where frequency[k][j] stores the frequency of characters ‘S[i]’ which lies in the series of index ‘k’ in K-periodic string, where ‘j’ = ‘i’ % ‘K’ for all 0 <= ‘i’ < N. And swap all the characters except the character which occurred maximum times in the series of ‘k-th’ index.