Nice problem using KMP.