Write a generator for substring pattern matches | Apple Interview Question