

You are given ‘STR’ = “aabac” and ‘QUERIES’ = [[1, 3], [2, 5]].
Then the answer of the first query will be 1 as we can convert the substring “aab” to “acb” by converting ‘a’ at index 2 to ‘c’ to get a pretty string.
The answer for the second query will be 1 as we can convert the substring “abac” to “cbac” by converting ‘a’ at index 2 to ‘c’.
The first line contains an integer 'T' which denotes the number of test cases.
The first line contains two space-separated integers, ‘N’ and ‘M’, denoting the length of the input string and the number of queries, respectively.
The second line of each test case contains a string denoting the input string.
The following ‘M’ lines contain two space-separated integers, ‘L’ and ‘R’, denoting the start and end index(1-indexed) of the substring of the string, respectively.
For each query, print the cost of the substring to the string from index ‘L’ to ‘R’.
The output for each test case will be printed in a separate line.
1 <= T <= 10
1 <= N <= 2000
‘STR’ contains only ‘a’, ‘b’ or ‘c’.
1 <= M <= 2000
1 <= L <= N
L <= R <= N
Time limit: 1 sec
You do not need to input or print anything, as it has already been taken care of. Just implement the given function.
There can be only six possible ways to create a pretty string:
So, we compare the input string with all the possible pretty strings and convert it into the pretty string, requiring a minimum number of operations. We will store the mismatched character count in a ‘DP’ table and answer the queries in O(1) using the ‘DP’ table. For example,
Now, if we have to find the cost substring of the input string from index ‘L’ to ‘R’, then the cost will be MINIMUM(‘DP[‘I’][‘R’] - ‘DP’[‘I’][‘L] - 1]) where ‘I’ is the ‘I’th pretty string.
Algorithm: