Apply the Knuth-Morris-Pratt algorithm to search for a given string (S) in reverse order (T). At each iteration, he will find the longest prefix S, which is the suffix T [1..i]. Then you just need to find the maximum lengths of these prefixes.
source
share