

On Valentine’s Day, Alice and Bob planned to go on a dinner date, but Alice is still unsure about Bob, so she decided to give him a task. She gave him a string ‘S’ of length ‘N’ containing only lowercase English letters and an integer ‘K’ and told him that he could:
Change some characters of ‘S’ to other lowercase letters (if required).
Divide ‘S’ into ‘K’ non-empty disjoint substrings such that each substring is a palindrome.
She asked Bob to find the minimum number of characters he needs to change to divide the given string in the required condition. Since Bob is busy preparing a perfect date for her, he called you to solve Alice’s challenge. Can you help Bob to impress her?
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’ and ‘K’. Here ‘N’ denotes the length of the string, and ‘K’ is an integer given by Alice.
The next line contains a string ‘S’ of length ‘N’. Here ‘S’ is the string given by Alice to Bob.
Output Format:
For each test case, print a single line containing a single integer denoting the minimum number of characters that Bob needs to change to divide ‘S’ into ‘K’ non-empty disjoint palindromic substrings.
The output of each test case will be printed in a separate line.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 50
1 <= K <= N
‘S’ contains only lowercase English letters.
Time limit: 1 sec.
2
6 2
coding
6 6
ninjas
2
0
Test Case 1 :
The string given by Alice is “coding” and ‘K’ = 2
One possible way is to change ‘d’ to ‘c’ and ‘g’ to ‘i’, the resulting string will be “cocini”
Now we can divide the new string into 2 substrings “coc” and “ini”
So the minimum required change is 2.
Test Case 2 :
The string given by Alice is “ninjas” and ‘K’ = 6
We can divide the string into 6 substrings of length 1.
So the minimum required change is 0.
1
6 3
aabbcc
0
Can you check all possible partitions to divide the given string into exactly ‘K’ non-empty disjoint substrings?
The idea here is to check all possible partitions such that it divides the given string into exactly ‘K’ non-empty disjoint substrings. We will try to divide the given string into ‘K’ non-empty disjoint substrings. We will calculate the minimum number of changes required to make it a palindrome for each substring and then we will take the sum of all ‘K’ substrings to get the minimum number of required changes for the whole string. Recursively we will try all possible divisions and compute their corresponding minimum number of changes. Finally, we will take a minimum of all such divisions.
Algorithm:
Description of ‘numChanges’ function
This function will take three parameters:
int numChanges(str, low, high):
Description of ‘allPartitions’ function
This function will take three parameters :
int allPartitions(low, high,k):
O(N * (2 ^ (N - 1) ) ), where ‘N’ is the length of the given string
There are (N * (N - 1) ) / 2 different substrings and it takes O(N) time to compute changes for each substring. Therefore it's O(N ^ 3) asymptotically.
In the worst case, ‘K’ can be up to ‘N’. Therefore there are 2 ^ (N - 1) possible ways to partition the given string (refer to this link for proof) and for each partitioning, it takes O(N)
So, the total time complexity will be O(N * 2 ^ (N - 1)) + O(N ^ 3) i.e O(N * 2 ^ (N - 1))
O(N ^ 2), where ‘N’ is the length of the given string.
It takes O(N ^ 2) space to store the changes for each substring.
Our ‘allPartitions’ function requires O(K) space in the form of recursive stack space, where ‘K’ is an integer given in the problem
So, the total space complexity will be O(N ^ 2) + O(K)