The Palindrome Game

easyPrefix sum + Hashmap
You are given a string $S$ of length $N$ consisting of lowercase English letters.

You will also be given $Q$ queries. Each query consists of two integers $L$ and $R$ ($1 \leq L \leq R \leq N$), asking whether the substring $S[L\,..\,R]$ (inclusive, 1-based indexing) can be rearranged to form a palindrome.

For each query, output and Yes if the substring can be rearranged to form a palindrome and No otherwise.

Input Format:

The first line contains two integers : length of string $N$ ($1 \leq N \leq 10^5$) and number of queries $Q$ ($1 \leq Q \leq 10^5$).
The second line contains string $S$ of length $N$ consisting only of lowercase English letters.
The next $Q$ lines contain two integers $L$ and $R$ ($1 \leq L \leq R \leq N$) --- the range of the substring to check for being a palindrome.

Output Format:

For each query, print Yes if the substring $S[L\,..\,R]$ can be rearranged to form a palindrome, otherwise print No. Each answer should be on a separate line.

Examples:

Example 1:

Input:

8 2
algopath
1 4
5 5

Output:

NO
YES

Example 2:

Input:

7 3
abacaba
1 7
2 4
3 5

Output:

YES
NO
YES

Code

Loading Editor...