Skip to content
Advertisement

Longest common substring from more than two strings

I’m looking for a Python library for finding the longest common sub-string from a set of strings. There are two ways to solve this problem:

  • using suffix trees
  • using dynamic programming.

Method implemented is not important. It is important it can be used for a set of strings (not only two strings).

Advertisement

Answer

These paired functions will find the longest common string in any arbitrary array of strings:

JavaScript

No doubt the algorithm could be improved and I’ve not had a lot of exposure to Python, so maybe it could be more efficient syntactically as well, but it should do the job.

EDIT: in-lined the second is_substr function as demonstrated by J.F. Sebastian. Usage remains the same. Note: no change to algorithm.

JavaScript

Hope this helps,

Jason.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement