Your team is participating in ICPC preliminaries. To qualify for the regional contest, you have to solve at least one problem of the competition. The easiest problem among them is that you have given a string ‘STR’ containing digits, and you have to find the longest substring of length 2P such that the sum of right ‘P’ elements and left ‘P’ elements are equal.
The first line of input contains an integer ‘T’, the number of test cases.
The only line in each test contains the string ‘STR’.
Output Format :
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.
Note :
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
2
1212
1538023
4
4
For first test case :
The length of the string is 4 and the sum of the first half and second half is the same.
For second test case the graph will be :
The longest even length substring with left half sum same as right half sum is “5380”.
Check every even length substring.
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 :
O(N ^ 3), where ‘N’ is the length of the string.
For each element in the string we traverse the string again to generate an even length substring. And for each even length substring we calculate the right half sum and left half sum which can take time maximum upto O(N). Therefore, the overall time complexity will be O(N ^ 3).
O(1)
We are not using any extra space. Therefore, the overall space complexity will be O(1).