![]() |
VOOZH | about |
Given two strings s1 and s2, return all distinct Longest Common Subsequences (LCS) in lexicographical order.
Examples:
Input: s1 = "abac", s2 = "aabca"
Output: ["aac", "aba", "abc"]
Explanation: Every subsequence that appears in both"abac"and"aabca"with the maximum possible length 3, are["aac" ,"aba", "abc"]Input: s1 = "baaa", s2 = "ca"
Output: ["a"]
Explanation: Every subsequence common to"baaa and"ca"of maximum length 1, is["a"].
Table of Content
Generate all subsequences of s1 using bitmask. Check each subsequence if it exists in s2 using two-pointer subsequence check. Track the maximum length and collect all distinct subsequences of that length.
["aac", "aba", "abc"]
Compute LCS length using DP table. Then backtrack to construct all distinct LCS strings by trying characters from 'a' to 'z' at each step, ensuring lexicographic order and only following paths that lead to full LCS length.
Illustration:
["aac", "aba", "abc"]