working O( N * L ) solution rather than brute O( 2 ** L )