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.

AnswerBot
4mo

The task is to find the minimum number of characters needed to be added to the front of a string to make it a palindrome.

  • Iterate through the string from both ends and count the number of characters th...read more

Help your peers!
Select
Add answer anonymously...

Paytm Full Stack Developer interview questions & answers

A Full Stack Developer was asked Q. Sort 0 1 2 Problem Statement Given an integer array arr of size 'N' containing o...read more
A Full Stack Developer was asked Q. What do you know about Paytm?
A Full Stack Developer was asked Q. Cousin Nodes in a Binary Tree Determine if two nodes in a binary tree are cousin...read more

Popular interview questions of Full Stack Developer

A Full Stack Developer was asked Q1. Sort 0 1 2 Problem Statement Given an integer array arr of size 'N' containing o...read more
A Full Stack Developer was asked Q2. What do you know about Paytm?
A Full Stack Developer was asked Q3. Cousin Nodes in a Binary Tree Determine if two nodes in a binary tree are cousin...read more
Paytm Full Stack Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits