Candies
Practice
4.2 (37 votes)
Algorithms
Binary search
Easy
Searching
String manipulation
Two pointer
Problem
81% Success 6054 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

You are given a string S consisting of lowercase English letters denoting different types of candies. A substring of a string S is a string S' that occurs in S. For example, "bam" is a substring of "babammm". Each candy costs 1 unit. You can pick some continuous candies such that you can create a palindrome of length K by using some or all picked candies. Your task is to find the minimum cost to create a palindrome of length K.

 

Input Format:

First line contains string S.

Next line contains an integer T denoting the number of test cases.

Next T lines contain a single integer K.

 

Output Format:

For each test case, print minimum cost as mentioned above. If you cannot create a palindrome of length K then, simply print -1.

 

Constraints:

\(1 \le |S| \le 10^5\\ 1\le T \le 10\\ 1\le K \le 10^5\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
38 votes
Tags:
ApprovedBinary SearchData StructuresEasyHash MapsHashing AlgorithmOpenSorting
Points:20
222 votes
Tags:
AlgorithmsApprovedBinary SearchEasyMathOpenSorting
Points:20
272 votes
Tags:
ApprovedEasyMathReadySorting