Minimum Characters to Make a String Palindrome

Given a string STR of length N, determine the minimum number of characters to be added to the front of the string to make it a palindrome.

Input:

The first line contains an integer 'T' denoting the number of test cases. Each of the next T lines contains a string STR. 

Output:

For each test case, output a single integer representing the minimum number of characters needed to make the string a palindrome, printed on a new line for each test case. 

Example:

Input:
2
deed
aabaaca
Output:
0
2
Explanation:

For the string "deed", it's already a palindrome, so no characters are needed. For "aabaaca", we need to add 'a' and 'c' to the front to form "acaabaaca", which is a palindrome.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 105
  • STR contains only lowercase English letters.

Note: You do not need to output anything yourself. Implement the function to compute the required values.

Be the first one to answer
Add answer anonymously...
Paytm Full Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter