Edit distance and LCS (Longest Common Subsequence)In standard Edit Distance where we are allowed 3 operations, insert, delete, and replace. Consider a variation of edit distance where we are allowed only two operations insert and delete, find edit distance in this variation. Examples: Input : str1 = "cat", st2 = "cut"Output : 2We are allowed to ins
6 min read
Longest Common Subsequence of two arrays out of which one array consists of distinct elements onlyGiven two arrays firstArr[], consisting of distinct elements only, and secondArr[], the task is to find the length of LCS between these 2 arrays. Examples: Input: firstArr[] = {3, 5, 1, 8}, secondArr[] = {3, 3, 5, 3, 8}Output: 3.Explanation: LCS between these two arrays is {3, 5, 8}. Input : firstAr
7 min read
Length of Longest Common Subsequence with given sum KGiven two arrays a[] and b[] and an integer K, the task is to find the length of the longest common subsequence such that sum of elements is equal to K. Examples: Input: a[] = { 9, 11, 2, 1, 6, 2, 7}, b[] = {1, 2, 6, 9, 2, 3, 11, 7}, K = 18Output: 3Explanation: Subsequence { 11, 7 } and { 9, 2, 7 }
15+ min read
Find the Longest Common Subsequence (LCS) in given K permutationsGiven K permutations of numbers from 1 to N in a 2D array arr[][]. The task is to find the longest common subsequence of these K permutations. Examples: Input: N = 4, K = 3arr[][] = {{1, 4, 2, 3}, {4, 1, 2, 3}, {1, 2, 4, 3}}Output: 3Explanation: Longest common subsequence is {1, 2, 3} which has leng
10 min read
Length of longest common prime subsequence from two given arraysGiven two arrays arr1[] and arr2[] of length N and M respectively, the task is to find the length of the longest common prime subsequence that can be obtained from the two given arrays. Examples: Input: arr1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}, arr2[] = {2, 5, 6, 3, 7, 9, 8} Output: 4 Explanation: The l
11 min read
A Space Optimized Solution of LCSGiven two strings, s1 and s2, the task is to find the length of the Longest Common Subsequence. If there is no common subsequence, return 0.Examples:Input: s1 = âABCDGHâ, s2 = âAEDFHRâOutput: 3Explanation: The longest subsequence present in both strings is "ADH".Input: s1 = âAGGTABâ, s2 = âGXTXAYBâO
13 min read
Longest common subarray in the given two arraysGiven two arrays A[] and B[] of N and M integers respectively, the task is to find the maximum length of an equal subarray or the longest common subarray between the two given array. Examples: Input: A[] = {1, 2, 8, 2, 1}, B[] = {8, 2, 1, 4, 7} Output: 3 Explanation: The subarray that is common to b
15+ min read
Longest subsequence such that adjacent elements have at least one common digitGiven an array arr[], the task is to find the length of the longest sub-sequence such that adjacent elements of the subsequence have at least one digit in common.Examples: Input: arr[] = [1, 12, 44, 29, 33, 96, 89] Output: 5 Explanation: The longest sub-sequence is [1 12 29 96 89]Input: arr[] = [12,
15+ min read
Longest subsequence such that difference between adjacents is oneGiven an array arr[] of size n, the task is to find the longest subsequence such that the absolute difference between adjacent elements is 1.Examples: Input: arr[] = [10, 9, 4, 5, 4, 8, 6]Output: 3Explanation: The three possible subsequences of length 3 are [10, 9, 8], [4, 5, 4], and [4, 5, 6], wher
15+ min read
Longest Increasing Subsequence using Longest Common Subsequence AlgorithmGiven an array arr[] of N integers, the task is to find and print the Longest Increasing Subsequence.Examples: Input: arr[] = {12, 34, 1, 5, 40, 80} Output: 4 {12, 34, 40, 80} and {1, 5, 40, 80} are the longest increasing subsequences.Input: arr[] = {10, 22, 9, 33, 21, 50, 41, 60, 80} Output: 6 Prer
12 min read