The following code fails. throwing an exception and producing no output.
The constraints on the input are 1<=n<=1000, 1<=k<=n and s.length is equal to n. It is also guaranteed that the input is exactly as specified.
Also, the code works, when 1<=n<=20.
def conforms(k,s):
    k = k + 1
    if s.find("0" * k) == -1 and s.find("1" * k) == -1:
        return True
    else:
        return False
def brute(n,k,s):
    min_val = n + 1
    min_str = ""
    desired = long(s,2)
    for i in range (2 ** n):
        xor = desired ^ i # gives number of bit changes
        i_rep = bin(i)[2:].zfill(n) # pad the binary representation with 0s - for conforms()
        one_count = bin(xor).count('1')
        if one_count < min_val and conforms(k, i_rep):
            min_val = bin(xor).count('1')
            min_str = i_rep
    return (min_val,min_str)
T = input()
for i in range(T):
    words = raw_input().split() 
    start = raw_input()
    sol = brute( int(words[0]), int(words[1]), start)
    print sol[0]
    print sol[1]