You are given two strings.Now you have to print all longest common sub-sequences in lexicographical order?
Input : str1 = "abcabcaa", str2 = "acbacba" Output: ababa abaca abcba acaba acaca acbaa acbca
This problem is an extension of longest common subsequence. We first find length of LCS and store all LCS in 2D table using Memoization (or Dynamic Programming). Then we search all characters from ‘a’ to ‘z’ (to output sorted order) in both strings. If a character is found in both strings and current positions of character lead to LCS, we recursively search all occurrences with current LCS length plus 1.
Below is the implementation of algorithm.
ababa abaca abcba acaba acaca acbaa acbca
This article is contributed by Shashak Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Length of Longest Balanced Subsequence
- Shortest Uncommon Subsequence
- Find all distinct subset (or subsequence) sums of an array
- Recursively break a number in 3 parts to get maximum sum
- Count All Palindromic Subsequence in a given String
- Maximum weight path ending at any element of last row in a matrix
- Number of permutation with K inversions
- String with additive sequence
- Count distinct occurrences as a subsequence
- Count Distinct Subsequences
- Printing Longest Common Subsequence | Set 2 (Printing All)
- Shortest Common Supersequence
- Longest Common Substring | DP-29
- Largest Independent Set Problem | DP-26
- Longest Common Subsequence | DP-4