Sequential Synergy — Problem Statement & Solution Guide
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
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'.
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
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;
}class Solution {
public:
string longestCommonSubsequence(string str1, string str2) {
int m = str1.length();
int n = str2.length();
vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
string longest = '';
for (int i = 1; i <= m; i++) {
for (int 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.substr(i - dp[i][j], i);
}
} else {
dp[i][j] = 0;
}
}
}
return longest;
}
};class Solution {
public String longestCommonSubsequence(String str1, String str2) {
int m = str1.length();
int n = str2.length();
int[][] dp = new int[m + 1][n + 1];
String longest = '';
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (str1.charAt(i - 1) == str2.charAt(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;
}
}def longest_common_subsequence(str1, str2):
m, n = len(str1), len(str2)
dp = [[0] * (n + 1) for _ in range(m + 1)]
longest = ''
for i in range(1, m + 1):
for j in range(1, n + 1):
if str1[i - 1] == str2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
if dp[i][j] > len(longest):
longest = str1[i - dp[i][j]:i]
else:
dp[i][j] = 0
return longestfunction 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.