![]() |
VOOZH | about |
Given a string 'str' of digits, find the length of the longest substring of 'str', such that the length of the substring is 2k digits and sum of left k digits is equal to the sum of right k digits.
Examples :
Input: str = "123123" Output: 6 The complete string is of even length and sum of first and second half digits is same Input: str = "1538023" Output: 4 The longest substring with same first and second half sum is "5380"
Simple Solution [ O(n3) ]
A Simple Solution is to check every substring of even length. The following is the implementation of simple approach.
Length of the substring is 4
Dynamic Programming [ O(n2) and O(n2) extra space]
The above solution can be optimized to work in O(n2) using Dynamic Programming. The idea is to build a 2D table that stores sums of substrings. The following is the implementation of Dynamic Programming approach.
Length of the substring is 4
Time complexity of the above solution is O(n2), but it requires O(n2) extra space.
[A O(n2) and O(n) extra space solution]
The idea is to use a single dimensional array to store cumulative sum.
Length of the substring is 6
[A O(n2) time and O(1) extra space solution]
The idea is to consider all possible mid points (of even length substrings) and keep expanding on both sides to get and update optimal length as the sum of two sides become equal.
Below is the implementation of the above idea.
Length of the substring is 6