Skip to content

Instantly share code, notes, and snippets.

@sammyrulez
Created December 30, 2011 13:34
Show Gist options
  • Save sammyrulez/1539883 to your computer and use it in GitHub Desktop.
Save sammyrulez/1539883 to your computer and use it in GitHub Desktop.
Equal Sum Substring
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')
@lathy88
Copy link

lathy88 commented Jul 9, 2015

Hi, May i know what the value you got for the second test case?
I got result as 74.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment