Strings
Strings
Strings
com/questions/6184869/what-is-the-difference-between-memoization
%20Programming,solving%20the%20problem%20bottom%2Dup .
https://www.geeksforgeeks.org/memoization-1d-2d-and-3d/?ref=rp
https://www.techiedelight.com/longest-common-subsequence/
https://www.techiedelight.com/longest-common-subsequence-lcs-space-optimized-version/
// If Last character of str1,str2 matches
if(str1[m-1] == str1[n-1])
return 1 + lcs(str1,str2,m-1,n-1);
// Else If last character of str1,str2 does not match
return max(lcs(str1,str2,m,n-1),lcs(str1,str2,m-1,n));
}
1) Time Complexity:-
Time complexity of the above naive recursive approach is O(2 m + n ) in the worst case
Worst Case happens when all characters of str1 and str2 mismatch
i.e. Worst Case occurs When length of Longest Common Subsequence is 0.
Each Recursive call will end up in 2 recursive calls
m = 6,n = 8………
In the Partial Recursion Tree Diagram lcs(str1,str2,6,8) (6,8)
Similarly lcs(str1,str2,5,8) represented as (5,8)
As we can see that the same subproblems(highlighted in the same color) are getting
computed again and again
https://www.techiedelight.com/longest-common-subsequence-of-k-sequences/
int lcs(string str1,string str2,string str3,int len1,int len2,int len3)
{
// Base Case :- If we have reached the end of either string
if ( (len1 == 0)||(len2==0)||(len3==0) )
return 0;
// if last character of str1,str2,str3 matches
if( (str1[len1-1]==str2[len2-1]) && (str2[len1-1]==str3[len3-1]) )
return 1 + lcs(str1,str2,str3,len1-1,len2-1,len3-1);
// else if last character of X, Y, Z don't match
return max( lcs(str1,str2,str3,len1-1,len2,len3),
lcs(str1,str2,str3,len1,len2-1,len3),
lcs(str1,str2,str3,len1,len2,len3-1) );
}
https://www.techiedelight.com/find-longest-common-prefix-lcp-strings/
https://www.geeksforgeeks.org/longest-common-prefix-using-divide-and-conquer-algorithm/?r
Concept:-
1) First write a function to find the longest commmon prefix b/w 2 strings
2) We’ll see that the Longest Common Prefix b/w 3 or more strings holds the associative prop
string longestCommonPrefix2(string str1,string str2)
{
int i=0,j=0;
int len1 = str1.length();
int len2 = str2.length();
while((i<len1) && (j<len2))
{
if(str1[i] != str2[j])
break;
i++,j++;
}
return str1.substr(0,i);// Or return str2.substr(0,i);
}
Q) KMP Algo
1) Pattern = a b c d a b c
Prefix = a,ab,abc,abcd
Suffix = c,
1) oni
Prefix = o,on
Suffix = i,ni
In other words , We cant take entire word as the Prefix or Suffix.
1) str = “AAAAABAAABA”
patt = “AAAA” 2) i=5,j=3
Hence lps = {0, 1, 2, 3} str = “AAAAABAAA
n = length of string patt = “AAAA”
m = length of pattern. str[i] , patt[j] don’t m
Change only j.
2) i=0,j=0 j = lps[j-1] = lps[3-1
str = “AAAAABAAABA”
patt = “AAAA” 2) i=5,j=2
str[i] , patt[j] match Do i++,j++ str = “AAAAABAAA
patt = “AAAA”
2) i=1,j=1 str[i] , patt[j] don’t m
str = “AAAAABAAABA” Change only j.
patt = “AAAA” j = lps[j-1] = lps[2-1
str[i] , patt[j] match Do i++,j++
2) i=5,j=1
2) i=2,j=2 str = “AAAAABAAA
str = “AAAAABAAABA” patt = “AAAA”
patt = “AAAA” str[i] , patt[j] don’t m
str[i] , patt[j] match Do i++,j++ Change only j.
j = lps[1-1] = lps[1-1
2) i=3,j=3
str = “AAAAABAAABA” 2) i=5,j=0
patt = “AAAA” str = “AAAAABAAA
str[i] , patt[j] match Do i++,j++ patt = “AAAA”
str[i] , patt[j] don’t m
2) i=4,j=4 Hence we do i++.
str = “AAAAABAAABA”
patt = “AAAA” 2) i=6,j=0
Now j == m A Pattern is found. str = “AAAAABAAA
Hence reset j patt = “AAAA”
j = lps[j-1] = lps[4-1] = lps[3] = 3. str[i] , patt[j] match
2) i=4,j=3 2) i=7,j=1
str = “AAAAABAAABA” str = “AAAAABA AA
patt = “AAAA” patt = “AAAA”
str[i] , patt[j] match Do i++,j++ str[i] , patt[j] match
https://www.techiedelight.com/find-lexicographic-permutations-string/
3) Then Fill the remaing cells of the table using the Formula:-
table(i,j) = 1 + table(i-1,j-1) …….If str1[i-1] == str2[j-1]
table(i,j) = max( table(i-1,j) , table(i,j-1) )……Otherwise
table(i,j) = 0……………………………………..If i,j = 0
5) In this table:-
Value in table[i][j] shows the length of LCS formed b/w str1[0..i-1] and str2[0..j-1]
So Value in table[m][n] shows the length of LCS formed b/w str1[0..m-1] and str2[0
// Function to find length of Longest Common Subsequence
// in str1[0..m-1] and str2[0..n-1]
int LCSLength(string str1, string str2,int m,int n)
{
// lookup table stores solution to already computed sub-problems
// i.e. lookup[i][j] stores the length of LCS of b/w strings
// str1[0..i-1] and str2[0..j-1]
int lookup[m + 1][n + 1];
int i,j;
// first column of the lookup table will be all 0
for (i = 0; i <= m; i++)
lookup[i][0] = 0;
// first row of the lookup table will be all 0
for (j = 0; j <= n; j++)
lookup[0][j] = 0;
// fill the lookup table in bottom-up manner
for (i = 1; i <= m; i++)
{
for (j = 1; j <= n; j++)
{
// if current character of X and Y matches
if (str1[i - 1] == str2[j - 1])
lookup[i][j] = 1 + lookup[i - 1][j - 1];
// else if current character of X and Y don't match
else
lookup[i][j] = max(lookup[i - 1][j], lookup[i][j - 1]);
}
}
// length of LCS b/w str1,str2 will be last entry in the lookup table
return lookup[m][n];
}
https://www.geeksforgeeks.org/minimum-insertions-to-form-a-palindrome-dp-28/