Your current code computes for one line for length L, in O(L^3) (substr takes linear runtime). Not to mention the high fixed costs of the aforementioned complexity due to inefficient passage around the lines.
Your algorithm can simply be reduced to searching for the longest common line prefixes with all its suffixes. This can be easily done using Suffix Aray . This concept cannot be explained as an answer, so I highly recommend that you read this .
Suffix Array's optimal and easy-to-code solution is O(Llg^2(L)) (L = line length) build time and O(1) time to request the longest common prefix of 2 suffixes using the Minimum range request . Note that the entire line itself is its own suffix. In your case, you need to make L queries for each row. Thus, the total complexity for one line will be O(Llg^2(L)) + O(L) .
If you want to improve even further, you can reduce the construction time to O(Llg(L)) using radix sorting or to O(L) ( Read )
source share