{"id":"b3a8bd41-cc48-44bb-82c5-bd7bd7db730a","name":"Say No To Palindrome","description":"\r\nLet's call the string beautiful if it does not contain a substring of length at least 2, which is a palindrome.\r\nLet's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first 3 letters of the latin alphabet (in lowercase).\r\nYou are given a string s of length n, each character of the string is one of the first 3 letters of the latin alphabet ( in lowercase).\r\nyou have to answer m queries - calculate the cost of the substring of the string s from l to r position, inclusive.","inputFormat":"The first line contains two integers n and m. the length of string s and the number of queries.\r\nThe second line contains the string s, it consists of n characters, each character one of the first 3 Latin letters.\r\nThe following m lines contain two integers l and r (both inclusive).","outputFormat":"For each query, print a single integer.\r\nthe cost of the substring of the string s from l-th to r-th position.","constraints":"1 <= n,m <= 200000\r\n1 <= l,r <= n","sampleCode":{"cpp":{"code":""},"java":{"code":"\r\nimport java.util.*;\r\nimport java.lang.*;\r\nimport java.io.*;\r\n\r\nclass Main {\r\n public int[] p;\r\n public int[] size;\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n }\r\n\r\n}\r\n"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"5 4\r\nbaacb\r\n1 3\r\n1 5\r\n4 5\r\n2 3\r\n","sampleOutput":"1\r\n2\r\n0\r\n1\r\n\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"a2073e25-96a1-4875-b400-f40bbe2edd95","name":"Text Processing For Experts","slug":"text-processing-for-experts-859","type":0},{"id":"69b9695f-ba89-4aca-ace1-4ca7dbbf5c38","name":"Say No To Palindrome","slug":"say-no-to-palindrome","type":1}],"next":{"id":"69a201c4-0167-4796-8f47-94965d7eaabf","name":"Coin Rows","type":1,"slug":"coin-rows"},"prev":{"id":"7c79106b-4334-43bf-bc83-d1ae6f23d397","name":"Prefix And Suffix Count","type":1,"slug":"prefix-and-suffix-count"}}}

Say No To Palindrome

Let's call the string beautiful if it does not contain a substring of length at least 2, which is a palindrome. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first 3 letters of the latin alphabet (in lowercase). You are given a string s of length n, each character of the string is one of the first 3 letters of the latin alphabet ( in lowercase). you have to answer m queries - calculate the cost of the substring of the string s from l to r position, inclusive.

{"id":"b3a8bd41-cc48-44bb-82c5-bd7bd7db730a","name":"Say No To Palindrome","description":"\r\nLet's call the string beautiful if it does not contain a substring of length at least 2, which is a palindrome.\r\nLet's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first 3 letters of the latin alphabet (in lowercase).\r\nYou are given a string s of length n, each character of the string is one of the first 3 letters of the latin alphabet ( in lowercase).\r\nyou have to answer m queries - calculate the cost of the substring of the string s from l to r position, inclusive.","inputFormat":"The first line contains two integers n and m. the length of string s and the number of queries.\r\nThe second line contains the string s, it consists of n characters, each character one of the first 3 Latin letters.\r\nThe following m lines contain two integers l and r (both inclusive).","outputFormat":"For each query, print a single integer.\r\nthe cost of the substring of the string s from l-th to r-th position.","constraints":"1 <= n,m <= 200000\r\n1 <= l,r <= n","sampleCode":{"cpp":{"code":""},"java":{"code":"\r\nimport java.util.*;\r\nimport java.lang.*;\r\nimport java.io.*;\r\n\r\nclass Main {\r\n public int[] p;\r\n public int[] size;\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n }\r\n\r\n}\r\n"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"5 4\r\nbaacb\r\n1 3\r\n1 5\r\n4 5\r\n2 3\r\n","sampleOutput":"1\r\n2\r\n0\r\n1\r\n\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"a2073e25-96a1-4875-b400-f40bbe2edd95","name":"Text Processing For Experts","slug":"text-processing-for-experts-859","type":0},{"id":"69b9695f-ba89-4aca-ace1-4ca7dbbf5c38","name":"Say No To Palindrome","slug":"say-no-to-palindrome","type":1}],"next":{"id":"69a201c4-0167-4796-8f47-94965d7eaabf","name":"Coin Rows","type":1,"slug":"coin-rows"},"prev":{"id":"7c79106b-4334-43bf-bc83-d1ae6f23d397","name":"Prefix And Suffix Count","type":1,"slug":"prefix-and-suffix-count"}}}
plane

Editor


Loading...

Say No To Palindrome

medium

Let's call the string beautiful if it does not contain a substring of length at least 2, which is a palindrome. Let's define cost of a string as the minimum number of operations so that the string becomes beautiful, if in one operation it is allowed to change any character of the string to one of the first 3 letters of the latin alphabet (in lowercase). You are given a string s of length n, each character of the string is one of the first 3 letters of the latin alphabet ( in lowercase). you have to answer m queries - calculate the cost of the substring of the string s from l to r position, inclusive.

Constraints

1 <= n,m <= 200000 1 <= l,r <= n

Format

Input

The first line contains two integers n and m. the length of string s and the number of queries. The second line contains the string s, it consists of n characters, each character one of the first 3 Latin letters. The following m lines contain two integers l and r (both inclusive).

Output

For each query, print a single integer. the cost of the substring of the string s from l-th to r-th position.

Example

Sample Input

5 4 baacb 1 3 1 5 4 5 2 3

Sample Output

1 2 0 1

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode