Apple Interview Question

Design the algorithm of grep