Tag Archives: LCS

LCS (Longest Common Subsequence) of three strings

Given 3 strings of all having length < 100,the task is to find the longest common sub-sequence in all three given sequences. Examples: Input : str1 = “geeks” str2 = “geeksfor” str3 = “geeksforgeeks” Output : 5 Longest common subsequence is “geeks” i.e., length = 5 Input : str1 = “abcd1e2” str2 = “bc12ea” str3… Read More »

Find if string is K-Palindrome or not | Set 2

Given a string, find out if the string is K-Palindrome or not. A K-palindrome string transforms into a palindrome on removing at most k characters from it. Examples: Input : String – abcdecba, k = 1 Output : Yes String can become palindrome by removing 1 character i.e. either d or e Input : String… Read More »

Printing Shortest Common Supersequence

Given two strings X and Y, print the shortest string that has both X and Y as subsequences. If multiple shortest supersequence exists, print any one of them. Examples: Input: X = “AGGTAB”, Y = “GXTXAYB” Output: “AGXGTXAYB” OR “AGGXTXAYB” OR Any string that represents shortest supersequence of X and Y Input: X = “HELLO”,… Read More »

A Space Optimized Solution of LCS

Given two strings, find the length of longest subsequence present in both of them. Examples: LCS for input Sequences “ABCDGH” and “AEDFHR” is “ADH” of length 3. LCS for input Sequences “AGGTAB” and “GXTXAYB” is “GTAB” of length 4. We have discussed typical dynamic programming based solution for LCS. We can optimize space used by… Read More »