
The first line of input contains an integer ‘T’, the number of test cases.
The only line in each test contains the string ‘STR’.
For each test case, print a single integer representing the length of the longest substring.
Print the output of each test case 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
1 <= |STR| <= 100
Time Limit : 1 sec
The basic idea is to check every even length substring of ‘STR’ and check whether the left half sum is equal to the right half sum or not.
Here is the algorithm :
The approach is to maintain an array to calculate the left sum and right sum in constant time. We create a sum array and store the prefix sum in it. Then for each element of the string, we generate a substring and calculate the left half and right half sum.
How to get the left half and right half sum using the prefix sum array?
Let the starting and ending points of a substring be ‘i’ and ‘j’ and ‘MID’ be the midpoint of the current substring.
Then left half sum will be : Sum till midpoint of substring - sum till starting point of a substring, i.e. ‘SUM[MID]’ - ‘SUM[i]’.
Similarly, we can find the right half sum using the substring's ‘MID’ and ending point (‘j’).
Here is the algorithm :
The approach is to consider every element of the string as the midpoint of the substring and then expand the substring while calculating the left half and right half sum.
Here is the algorithm :