


For the given string “deed”, the string is already a palindrome, thus, minimum characters needed to be added are 0.
Similarly, for the given string “abb”, the minimum characters needed is 1 i.e. ‘a’ which is to be added to the end of the string, which makes the string “abba”, which is a 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 contains the string 'STR'.
For each test case, print the count of minimum characters needed to make the string palindrome.
Output for each test case will be printed in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10 ^ 2
1 <= N <= 10 ^ 4
'STR' contains only lowercase English letters.
Time limit: 1 second
For an interval [ ‘l’, ‘r’ ], in order to make it a palindrome using minimum insertions we have to consider the following cases-
We can write a recursive solution based on these observations.
The algorithm will be:
We can use memoization to optimize the recursive approach. Since many recursive calls have to be made with the same parameters, this redundancy can be eliminated by storing the results obtained for a particular call in memoization in a matrix ‘MEMO’.
We can also solve this problem with the help of dynamic programming similar to previous approaches.
The algorithm will be: