Skip to main content
CodeOath
← All problems

Problem

Longest Common Subsequence

Medium
  • dynamic-programming
  • strings

Return the length of the longest sequence of characters that appears, in the same order, in both text1 and text2. The characters do not have to be next to each other in either string. If the two strings share no character, return 0.

Example 1
Input
text1 = "nightfall", text2 = "flight"
Output
4
Explanation

ight appears in both words in the same order. The f and l of flight come before it, but in nightfall they come after it, so they cannot be added.

Example 2
Input
text1 = "abc", text2 = "cba"
Output
1
Explanation

the letters are the same but in opposite order, so only one of them can be kept.

Example 3
Input
text1 = "kiwi", text2 = "plum"
Output
0
Explanation

the two words have no letter in common.

Constraints:

  • 1 <= text1.length, text2.length <= 1000

Tab indents. Press Esc, then Tab to leave the editor.

Run your code to see every test here. Nothing is submitted or recorded.