AlgoUniversity
  • Go Back
Discussion
Good Strings :

Author

Gurankit Pal Singh

Difficulty Level : Medium

Submissions : 281

Asked In : Amazon

Marks :10

: 8 | : 0

You are given a string $$$s$$$ of length $$$n$$$, $$$q$$$ ranges of the form $$$l, r$$$ , and an array $$$arr$$$, containing permutation of numbers from 1 to $$$n$$$.

In one operation, you remove the first unremoved character as per the permutation. However the positions of the other characters will not change.

A string is considered good, if all the $$$q$$$ ranges have no repeated characters, after ignoring the removed ones. Please note that a range with all characters removed is also considered to be good.

Determine the minimum number of operations required to make the string good.

Input

The first line contains two space separated integers $$$n$$$ and $$$q$$$. $$$(1 \le n,q \le 10^5)$$$

The second line contains the string $$$s$$$.

The third line contains $$$n$$$ space separated integers denoting the permutation $$$arr$$$.

The next $$$q$$$ lines contain 2 integers $$$l$$$ and $$$r$$$.$$$(1 \le l \le r \le n)$$$

Output

Print an integer denoting the minimum number of operations required to make the string good.

Examples

Input
5 2
aaaaa
2 4 1 3 5
1 2
4 5
Output
2
Input
8 3
abbabaab
6 3 5 1 4 2 7 8
1 3
4 7
3 5
Output
5

Note

For the first sample case, after 2 operations, the string will become "a.a.a" ('.' denote removed characters).

So the ranges $$$(1,2)$$$, and $$$(4,5)$$$ both contain single occurrence of 'a' Hence, the answer is 2.

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.