


‘SECRETCODE’ consists of only English uppercases.
The first line of input contains an integer 'T' representing the number of test cases or queries to be processed. Then the test case follows.
The first line of each test case contains a single integer ‘N’ where ‘N’ denotes the length of 'SECRETCODE'.
The second line of each test case contains the string 'SECRETCODE'.
For each test case, print a single integer denoting the length of longest palindromic subsequence present in the given string 'SECRETCODE'.
Print the output of each test case in a separate line.
Note :
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 10^3
Where ‘T’ is the total number of test cases and ‘N’ denotes the length of 'SECRETCODE'
Time limit: 1 second
We use recursion to solve this problem. If the two ends of a string are the same, then they must be included in the longest palindrome subsequence. Otherwise, both ends cannot be included in the longest palindrome subsequence.
Here is the complete algorithm -
We are solving this problem by solving its subproblems and then combining their solutions. If we analyze carefully, we will see that we are solving the same subproblems multiple times. Thus, this problem exhibits overlapping subproblems. So, in this approach, we will eliminate the need for solving the same subproblems again and again by using memoization.
The algorithm is similar to the previous approach with some additions.
We create a 2D table ‘lookUp’ of size N * N (where ‘N’ denotes the length of 'SECRETCODE') to store the solutions to the subproblems. lookUp[ i ][ j ] denotes the longest palindromic subsequence for substring from ‘i’ to ‘j’ ’ We initialize the table by -1.
We implement the above idea in the bottom-up approach. We try all the possible substring lengths and compute the answer for each of them and then update the answer in ‘dp’ matrix.
The idea is to create a 2-D array ‘dp’ of size (N + 1)*N initialised to 0. dp[i][j] denotes the maximum length palindromic subsequence of the substring of the ‘SECRETCODE’ from ‘i’ to ‘j’.
We compute the value for each substring starting from ‘1’ to ‘N’ where ‘N’ is the length of ‘SECRETCODE’.