The % test (I would have just min()) is fine if you just add a |= len==len at the end.