Skip to content

Instantly share code, notes, and snippets.

@opethe1st
Last active April 13, 2017 17:00
Show Gist options
  • Save opethe1st/332f872cceba4913b93f4a559e9e11c8 to your computer and use it in GitHub Desktop.
Save opethe1st/332f872cceba4913b93f4a559e9e11c8 to your computer and use it in GitHub Desktop.
def flip(s):
res = []
for l in s:
if l=='+':
res.append('-')
else:
res.append('+')
return "".join(res)
def countFlips(s,k):
count = 0
for i in xrange(len(s)-k+1):
#print s
if s[i]=='-':
s = s[:i]+flip(s[i:i+k])+s[i+k:]
count+=1
if s.count('+')==len(s):
return count
else:
return -1
#tests
print countFlips('---+-++-',3) #this should be 3
print countFlips('+++++', 4) #this should be 0
print countFlips('-+-+-', 4) #this should be -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment