Created
December 30, 2011 13:34
-
-
Save sammyrulez/1539883 to your computer and use it in GitHub Desktop.
Equal Sum Substring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getEqualSumSubstring (s): | |
substrings = get_all_subsets(s) | |
for subsr in substrings: | |
if check_string(subsr): | |
return len(subsr) | |
return 0 | |
def get_all_subsets(set_val): | |
out = [set_val] | |
for i in range(0,len(set_val)): | |
for k in range(i,len(set_val)): | |
append_id_even(set_val[i:k],out) | |
out.sort(key=len) | |
out.reverse() | |
return out | |
def append_id_even(str_t,list_t): | |
if len(str_t) > 2 and len(str_t)%2==0: | |
list_t.append(str_t) | |
def check_string(str): | |
mid = len(str) / 2 | |
first = 0; | |
second = 0; | |
for i in range(0,mid): | |
first = first + int(str[i]) | |
second = second + int(str[mid + i]) | |
return first == second | |
def test(): | |
print getEqualSumSubstring('123231') | |
print getEqualSumSubstring('986561517416921217551395112859219257312') | |
print getEqualSumSubstring('984') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, May i know what the value you got for the second test case?
I got result as 74.