Longest Common Subsequence
MediumPython 3Dynamic programmingStringsSubsequences
A subsequence of a string keeps characters in their original order but not necessarily contiguous ("ELO" is a subsequence of "HELLO").
You are given two strings s1 and s2. Return a longest common subsequence of the two: a string that is a subsequence of both, of maximum possible length. If several subsequences tie for the maximum length, any one of them is accepted. If the strings share no characters, return "".
Constraints: - 0 <= len(s1), len(s2) <= 1000
Examples:
longest_common_subsequence("HELLO", "MELODY") == "ELO"
longest_common_subsequence("ABCBDAB", "BDCABA") -> length 4 (e.g. "BCBA")
longest_common_subsequence("ABC", "XYZ") == ""
longest_common_subsequence("ABC", "ABC") == "ABC"Python 3, run in your browser. Run tests runs the problem's own tests with a free account; the AI interviewer reads your code and your runs in a practice interview.
Loading the editor…