3

What is the best way to find maximum number of consecutive common elements (or length of longest common consecutive sequence?) in two lists of strings in Python?

For example:

List1 = ['sun', 'moon', 'flower', 'fruit', 'apple', 'star', 'rose', 'fruit', 'lily', 'fairy']

fxn(List1, ['hercules', 'flower', 'fruit', 'rose'])         Returns 2 (flower, fruit)
fxn(List1, ['flower', 'fruit', 'apple', 'star'])            Returns 4
fxn(List1, ['apple', 'rose', 'flower', 'fruit', 'apple'])   Returns 3 (flower, fruit, apple)
fxn(List1, ['moon', 'apple', 'fairy'])                      Returns 1

I hope I have worded the question well. The sample inputs and corresponding expected output might help. I did my best at searching for a duplicate, but haven't found any. Let me know if there is one.

MSeifert
  • 145,886
  • 38
  • 333
  • 352
lstephe1
  • 31
  • 3
  • Similar question?: https://stackoverflow.com/questions/13628541/how-to-find-longest-common-subarray – akp Aug 24 '17 at 05:30
  • @wim This question asks about longest common **contiguous** subsequences. It's not **much** different but it's a different problem. – MSeifert Aug 24 '17 at 06:11
  • @MSeifert I'm aware of that, but the dupe posted has an answer from Martijn which demonstrates **both** longest common contiguous subsequence and longest common subsequence. You didn't notice that? – wim Aug 24 '17 at 16:23
  • @wim You're right. I just looked at the question. And I thought duplicates should be chosen according to the question **not the answer**. But given that my solution is almost the same as Martijns I duped it again. – MSeifert Aug 24 '17 at 16:34
  • Dupe banner reads "This question already has an answer here". The questions themselves needn't match exactly. – wim Aug 24 '17 at 16:36

0 Answers0