Input: N = 5, Q = 2, S = "aaaaa", arr[] = {2, 4, 1, 3, 5}, ranges = {{1, 2}, {4, 5}}
Output: 2
Explanation: After the first operation, the string becomes a_aaa
After the second operation, the string becomes a_a_a
Now, in both ranges, all characters are distinct.
Hence, the output is 2.
Input: N = 8, Q = 3, S = "abbabaab", arr[] = {6, 3, 5, 1, 4, 2, 7, 8}, ranges = {{1, 3}, {4, 7}, {3, 5}}
Output: 5
Explanation: After the first operation, the string becomes abbab_ab
After the second operation, the string becomes ab_ab_ab
After the third operation, the string becomes ab_a_ _ab
After the fourth operation, the string becomes _b_a_ _ab
After the fifth operation, the string becomes _b_ _ _ _ab
Now, in all the ranges, all characters are distinct.
Hence, the output is 5.
Below is the implementation of the above approach.