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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Print all the combinations of a string in lexicographical order
- Print all distinct circular strings of length M in lexicographical order
- Generate all numbers up to N in Lexicographical Order
- Find the k-th string in lexicographical order consisting of n-2 X's and 2 Y's
- Sort the words in lexicographical order in Python
- Print all lexicographical greater permutations of a given string
- Generating distinct subsequences of a given string in lexicographic order
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Print all subsequences of a string
- Longest Common Subsequence | DP-4
- Longest Common Substring | DP-29
- Print all subsequences of a string using ArrayList
- Print all subsequences of a string in Python
- Longest Common Increasing Subsequence (LCS + LIS)
- Longest common subarray in the given two arrays
- LCS (Longest Common Subsequence) of three strings
- Longest Common Anagram Subsequence
- C++ Program for Longest Common Subsequence
- Longest Common Subsequence with at most k changes allowed
- Longest Common Subsequence | DP using Memoization
Improved By : mohit kumar 29