Longest Even Length Substring Sum
Given a string consisting of digits (0-9), find the length of the longest contiguous substring that is even in length and whose first half and second half have equal sum.
[ "123123" ]
Explanation. The entire string meets the conditions, with halves '123' and '123' having the same sum of 6.
[ "1538023" ]
Explanation. The longest substring that meets the condition is '5380' with halves '53' and '80' both having the sum of 8.
[ "123456" ]
Explanation. The only substring meeting the condition is generated by any two consecutive same digits.
[ "1122" ]
Explanation. The entire string meets the conditions, with halves '11' and '22' having the same sum of 2.
[ "000000" ]
Explanation. The entire string meets the conditions, with any split having the same sum of 0.
[ "1234" ]
Explanation. No even length substrings have equal halves in sums.
Follow-up: Can you optimize your solution to run in \(O(n^2)\) time complexity?
The input string will not be empty and will contain only digits from \(0-9\).
- Views
- 3