LongestCommonSequence

LongestCommonSequence[s1,s2]

finds the longest sequence of contiguous or disjoint elements common to the strings, lists or biomolecular sequences s1 and s2.

Details and Options

  • LongestCommonSequence returns the longest common sequence with disjoint pieces concatenated.
  • If there are several common subsequences of the same length, LongestCommonSequence returns the one that begins earliest in s1.
  • For strings, setting the option IgnoreCase->True makes LongestCommonSequence treat lowercase and uppercase letters as equivalent, and return the form of common sequence that occurs in s1.

Examples

open allclose all

Basic Examples  (3)

Find the longest noncontiguous sequence common to two strings:

Find the longest noncontiguous sequence common to two lists:

Find the longest noncontiguous sequence common to two biomolecular sequences:

Neat Examples  (1)

Find the lengths of longest common sequences in random binary sequences of length 100:

Wolfram Research (2008), LongestCommonSequence, Wolfram Language function, https://reference.wolfram.com/language/ref/LongestCommonSequence.html (updated 2020).

Text

Wolfram Research (2008), LongestCommonSequence, Wolfram Language function, https://reference.wolfram.com/language/ref/LongestCommonSequence.html (updated 2020).

CMS

Wolfram Language. 2008. "LongestCommonSequence." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/LongestCommonSequence.html.

APA

Wolfram Language. (2008). LongestCommonSequence. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/LongestCommonSequence.html

BibTeX

@misc{reference.wolfram_2023_longestcommonsequence, author="Wolfram Research", title="{LongestCommonSequence}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/LongestCommonSequence.html}", note=[Accessed: 19-March-2024 ]}

BibLaTeX

@online{reference.wolfram_2023_longestcommonsequence, organization={Wolfram Research}, title={LongestCommonSequence}, year={2020}, url={https://reference.wolfram.com/language/ref/LongestCommonSequence.html}, note=[Accessed: 19-March-2024 ]}