Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

167. Longest Common Subsequence

MediumDay 48 of 60Dynamic Programming

Given two strings text1 and text2, return the length of their longest common subsequence, the longest string that is a subsequence of both. A subsequence deletes some characters (possibly none) without changing the order of the rest. Return 0 if they share no characters.

Example 1
Input: text1 = "abcde", text2 = "ace"
Output: 3
"ace" appears in both.
Example 2
Input: text1 = "abc", text2 = "def"
Output: 0
Constraints
  • 1 <= text1.length, text2.length <= 1000
  • Both strings are lowercase English letters
Hints
Frequently asked at: Amazon, Google, Microsoft, Goldman Sachs
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min