filter(function, iterator)
def isvalid(s):
ctr = 0
for c in s:
if c == '(':
ctr += 1
elif c == ')':
ctr -= 1
if ctr < 0:
return False
return ctr == 0
level = {s}
while True:
# removfe items in level which are not valid
valid = filter(isvalid, level)