# Minimum operations such that each given segment of String have distinct characters

Given a string** S** of length **N**, **Q** ranges of the form **[L, R]** in a **2-D** array range, and a permutation **arr[]** containing numbers from **1** to **N**. In one operation, you can remove the first unremoved character as per the permutation. However, the positions of other characters will not change. Determine the minimum number of operations that follow the below conditions:

- All the characters in each of the
**Q**ranges are distinct. Removed characters are not counted. - A range with all characters removed is considered to have all distinct characters.
- The sequence of
**n**integers is called a permutation if it contains all integers from**1**to**n**exactly once.

**Note:** 1-based indexing is followed.

**Examples:**

Input:N = 5, Q = 2, S = “aaaaa”, arr[] = {2, 4, 1, 3, 5}, ranges = {{1, 2}, {4, 5}}Output:2Explanation: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:5Explanation: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.

**Approach: **To solve the problem follow the below idea:

One easy way is to traverse the permutation and remove characters serially. At each traversal, we will traverse each query to find whether they all contain unique characters. If the answer comes to be positive then return the number of traversals is the final answer.

Follow the below steps to solve the problem:

- If all the queries have unique characters then return
**0**. - Traverse the permutation
**arr[]**from**i = 0 to N-1**:- Initialize
**(arr[i] – 1)th**index with**‘_’**in a list**temp**and call**isValid**. - Inside
**isValid**function:- Traverse the query
**ranges**using iterator**j**.- If
**temp[j] != ‘_’**then push**temp[j]**in**ss**). - If the length of the set formed by
**ss**is not equal to the length of**ss**then there are repeating characters so return**False**. - Else return
**True**.

- If

- Traverse the query
- If
**isValid**returns**True**then return**i+1**as the final answer. - Otherwise, return
**-1**.

- Initialize

Below is the implementation of the above approach.

## Python3

`# Python program for the above approach:` `def` `IsValid(temp, ranges):` ` ` `for` `i ` `in` `range` `(` `len` `(ranges)):` ` ` `x, y ` `=` `ranges[i]` ` ` `ss ` `=` `[]` ` ` `for` `j ` `in` `range` `(x ` `-` `1` `, y):` ` ` `if` `temp[j] !` `=` `'_'` `:` ` ` `ss.append(temp[j])` ` ` `if` `len` `(` `set` `(ss)) !` `=` `len` `(ss):` ` ` `return` `False` ` ` `return` `True` ` ` `def` `goodString (N, Q, S, arr, ranges):` ` ` `temp ` `=` `list` `(S)` ` ` `if` `IsValid(temp, ranges):` ` ` `return` `0` ` ` `for` `i ` `in` `range` `(` `len` `(arr)):` ` ` `temp[arr[i] ` `-` `1` `] ` `=` `'_'` ` ` `if` `IsValid(temp, ranges):` ` ` `return` `i ` `+` `1` ` ` `return` `-` `1` ` ` `# driver's code` `N ` `=` `8` `Q ` `=` `3` `S ` `=` `"abbabaab"` `arr ` `=` `[` `6` `, ` `3` `, ` `5` `, ` `1` `, ` `4` `, ` `2` `, ` `7` `, ` `8` `]` `ranges ` `=` `[[` `1` `, ` `3` `], [` `4` `, ` `7` `], [` `3` `, ` `5` `]]` ` ` `print` `(goodString(N, Q, S, arr, ranges))` |

**Output**

5

**Time Complexity: **O(N * Q * M) where M is the maximum range**Auxiliary Space: **O(N)