


Given a string STR of length N. The task is to return the count of minimum characters to be added at front to make the string a palindrome.
For example, for the given string “deed”, the string is already a palindrome, thus, minimum characters needed are 0.
Similarly, for the given string “aabaaca”, the minimum characters needed are 2 i.e. ‘a’ and ‘c’ which makes the string “acaabaaca” palindrome.
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then, the T test cases follow.
The first and only line of each test case or query contains the string STR.
Output format :
For each test case, print the count of minimum characters needed in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10 ^ 5
STR contains only lowercase English letters.
Time limit: 1 sec
2
abcd
dad
3
0
For test case 1 :
Minimum characters to be added at front to make it a palindrome are 3 i.e. “dcb” which makes the string “dcbabcd”.
For test case 2 :
The string is already a palindrome, we do not need to add any character.
2
xxaxxa
abb
1
2
First off, observe that the question focuses on the terms palindrome and addition at front. We have to make a string a palindrome when we are allowed to add characters only at front.
O(N ^ 2) per test case, where N is the size of the given string.
In the worst case, for N number of times, we are checking if the string is a palindrome of not taking O(N) time.
O(1) per test case,
In the worst case, only constant extra space is used.