FrontendChallenges is a collection of frontend interview questions and answers. It is designed to help you prepare for frontend interviews. It's free and open source.
difficulty: medium
title: Longest Common Subsequence
type: question
template: typescript
tags: javascript, blind75, dynamic-programming, string
Question
Given two strings text1 and text2, find the length of their longest common subsequence.
Rules:
Subsequence can skip characters (doesn't need to be contiguous)
Must maintain relative order of characters
Common subsequence must exist in both strings
Return 0 if no common subsequence exists
Constraints:
1 ≤ text1.length, text2.length ≤ 1000
Strings contain only lowercase English letters
Examples:
// Example 1:
console.log(longestCommonSubsequence("cat", "crabt"));
// Output: 3
// Explanation: "cat" is common subsequence
// Example 2:
console.log(longestCommonSubsequence("abcd", "abcd"));
// Output: 4
// Explanation: Entire string is common
// Example 3:
console.log(longestCommonSubsequence("abcd", "efgh"));
// Output: 0
// Explanation: No common subsequence
Template
index.ts
export function longestCommonSubsequence(text1: string, text2: string): number {
}
Info
Question
Given two strings
text1
andtext2
, find the length of their longest common subsequence.Rules:
Constraints:
Examples:
Template
index.ts
index.test.ts