


Input: ‘S’ =’badam’
Output: ‘ada’
‘ada’ is the longest palindromic substring, and it can be proved that it is the longest possible palindromic substring.
First-line contains 'T', denoting the number of Test cases.
For each Test case:
The first line contains an integer ‘N’ denoting the string ‘S’ length.
The second line contains the string ‘S’, consisting of lowercase English alphabets.
Return the longest palindromic substring in ‘S’.
You don't need to print anything. Just implement the given function.
1 <= 'T' <= 10
1 <= 'N' <= 10^5
‘a’ <= ‘S[i]’ <= ‘Z’
Time Limit: 1 sec
The basic idea over here is to check for each possible substring of ‘S’ and verify whether it is a palindrome or not.
Pick all possible starting and ending positions, and check if it is a palindrome. If it is a palindrome, add one to the answer and finally return the answer after checking all substrings.
In the brute force solution, we do unnecessary recomputation for verifying palindromes. For example, if we know the string ‘dad’ is a palindrome, then ‘cdadc’ must be a palindrome as the left, and right characters are equal.
Hence we can store some information in a DP state to avoid this recomputation.
State- DP[i][j] = whether the substring S[i:j] is a palindrome or not.
Transition- DP[i][j] = DP[i-1][j-1] AND S[i] == S[j]
Base Cases:
One character DP[i][i]=True
Two character DP[i][i+1]= ( S[i] == S[i+1] )