KMP algorithm, elegant