Kmp prefix table. This blog explains it really beautifully - The Knuth-Morris-Pratt Algorit...