BackhardStrings

Sequential Synergy Solution

Problem Statement

Given two sequences of unique symbols, devise a method to identify the longest contiguous or non-contiguous subsequence of symbols common to both. If there are multiple such subsequences, return the first one encountered.

Example 1
Input
['A', 'B', 'C', 'D'], ['A', 'D', 'E', 'F']
Output
A

Explanation: Step-by-step: we first find the longest contiguous common subsequence 'A'. Then we find the longest non-contiguous common subsequence 'A' and 'D'. Since 'A' is longer, the output is 'A'.

Example 2
Input
['Y', 'Z', 'A', 'B'], ['Y', 'A', 'C', 'D']
Output
Y

Explanation: Step-by-step: we first find the longest contiguous common subsequence 'Y'. Then we find the longest non-contiguous common subsequence 'Y' and 'Z'. Since 'Y' is longer, the output is 'Y'.

Constraints

  • Each sequence consists of unique symbols ranging from A to Z and 0 to 9.
  • The sequences can undergo changes, with symbols being added, removed, or modified over time.
  • 0 <= sequence length <= 1000
  • The sequences do not contain duplicate symbols.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Sequential Synergy — Problem Statement & Solution Guide

StringsHardLongest Common Subsequence
TimeO(n*m)
|
SpaceO(n*m)

Problem Description

Given two sequences of unique symbols, devise a method to identify the longest contiguous or non-contiguous subsequence of symbols common to both. If there are multiple such subsequences, return the first one encountered.

Examples

Example 1

Input

['A', 'B', 'C', 'D'], ['A', 'D', 'E', 'F']

Output

A

Explanation: Step-by-step: we first find the longest contiguous common subsequence 'A'. Then we find the longest non-contiguous common subsequence 'A' and 'D'. Since 'A' is longer, the output is 'A'.

Example 2

Input

['Y', 'Z', 'A', 'B'], ['Y', 'A', 'C', 'D']

Output

Y

Explanation: Step-by-step: we first find the longest contiguous common subsequence 'Y'. Then we find the longest non-contiguous common subsequence 'Y' and 'Z'. Since 'Y' is longer, the output is 'Y'.

Constraints

  • Each sequence consists of unique symbols ranging from A to Z and 0 to 9.
  • The sequences can undergo changes, with symbols being added, removed, or modified over time.
  • 0 <= sequence length <= 1000
  • The sequences do not contain duplicate symbols.

Optimal Approach & Strategy

The optimized solution utilizes dynamic programming to track the lengths of common subsequences in a 2D array, where each cell represents the length of the longest common subsequence up to that point in the sequences. This approach allows for a time complexity of O(n*m), where n and m are the lengths of the sequences.

Brute Force Approach

A naive approach would involve generating all possible subsequences of both sequences and comparing them to find the longest common one. However, this is highly inefficient due to the exponential number of subsequences. Another approach could involve using recursion to compare all possible subsequences, but this also results in significant redundancy and inefficiency.

Verified Code Solutions

JavaScript Solution
Time: O(n*m)
function longestCommonSubsequence(str1, str2) {
   const m = str1.length;
   const n = str2.length;
   const dp = Array(m + 1).fill(0).map(() => Array(n + 1).fill(0));
   let longest = '';
   for (let i = 1; i <= m; i++) {
       for (let j = 1; j <= n; j++) {
           if (str1[i - 1] === str2[j - 1]) {
               dp[i][j] = dp[i - 1][j - 1] + 1;
               if (dp[i][j] > longest.length) {
                   longest = str1.substring(i - dp[i][j], i);
               }
           } else {
               dp[i][j] = 0;
           }
       }
   }
   return longest;
}

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.