At 28:08, in lpsBottomUp function, after initializing array, add following to make all values of diagonal to 1: for (int i = 0; i < str.length(); i++) { arr[i][i] = 1; } - I've corrected in Source code: thecodingsimplified.com/longest-palindromic-subsequence/
int a=palan(s,i+1,j,dp); int b=palan(s,i,j-1,dp); return dp[i][j]=Math.max(a,b); } thats my code for Lps for number problem is that's i want to generate palindrom string using 2d dp array can you do it plz any other can do then comment the code.
Hi, The Topdown approach has failed some of the test cases, below is the one of the test cases tkjprepggxrpnrvystmwcysyycqpevikeffmznimkkasvwsrenzkycxfxtlsgypsfadpooefxzbcoejuvpvaboygpoeylfpbnpljvrvipyamyehwqnqrqpmxujjloovaowuxwhmsncbxcoksfzkvatxdknlyjyhfixjswnkkufnuxxzrzbmnmgqooketlyhnkoaugzqrcddiuteiojwayyzpvscmpsajlfvgubfaaovlzylntrkdcpwsrtesjwhdizcobzcnfwlqijtvdwvxhrcbldvgylwgbusbmborxtlhcsmpxohgmgnkeufdxotogbgxpeyanfetcu Code's output is: 108 It's Correct output is: 107 Thank you very much for the explanation!!
I would have visited this channel before I wasted my interview preparation time by looking ambiguous lectures from other sources, as I am not getting the core of DP, Greedy, recursion etc..
Excellent, i like the explanation and different approaches. However, how do you print the string efficiently? I see alot of videos miss out on this part.
why you add +2 if they are equal on bottum up approach for string abba longest substring is 4 but using this approach we initialize to 1 and coninue so adding 2 to this will.not give 4 Could you reply on this
You can even start from top, if you start from top, it'll fill left diagonal side. There's no specific reason. Just that we want to cover basic to complete string, so any one of diagonal you need to fill. Left or right diagonal side.
public int longestPalindromeSubseq(String s) { String dp[][] = new String[s.length()+1][s.length()+1]; String ans = solve(s,0,s.length()-1,dp); return ans.length(); } }
At 28:08, in lpsBottomUp function, after initializing array, add following to make all values of diagonal to 1:
for (int i = 0; i < str.length(); i++) {
arr[i][i] = 1;
}
- I've corrected in Source code: thecodingsimplified.com/longest-palindromic-subsequence/
int palan(String s,int i,int j,int dp[][]){
if(i>j)
return 0;
if(i==j){
return dp[i][j]= 1;
}
if(dp[i][j]!=-1){
return dp[i][j];
}
if(s.charAt(i)==s.charAt(j)){
return dp[i][j]= 2+palan(s,i+1,j-1,dp);
}
int a=palan(s,i+1,j,dp);
int b=palan(s,i,j-1,dp);
return dp[i][j]=Math.max(a,b);
}
thats my code for Lps for number problem is that's i want to generate palindrom string using 2d dp array can you do it plz any other can do then comment the code.
Best explanation of this problem I could find!
Thanks for your nice feedback. Keep Watching
Thank you very much sir, This is very helpful and please never stop your teaching, it will help so many people
Thanks for your nice feedback. Keep Watching.
Really helpful for java students. Kudos to your great work!
Thanks for your nice feedback. Keep watching out other videos as well.
Hi,
The Topdown approach has failed some of the test cases, below is the one of the test cases
tkjprepggxrpnrvystmwcysyycqpevikeffmznimkkasvwsrenzkycxfxtlsgypsfadpooefxzbcoejuvpvaboygpoeylfpbnpljvrvipyamyehwqnqrqpmxujjloovaowuxwhmsncbxcoksfzkvatxdknlyjyhfixjswnkkufnuxxzrzbmnmgqooketlyhnkoaugzqrcddiuteiojwayyzpvscmpsajlfvgubfaaovlzylntrkdcpwsrtesjwhdizcobzcnfwlqijtvdwvxhrcbldvgylwgbusbmborxtlhcsmpxohgmgnkeufdxotogbgxpeyanfetcu
Code's output is: 108
It's Correct output is: 107
Thank you very much for the explanation!!
Thanks a lot. This question was hitting me from the last week. Thanks a lot
Glad it was helpful!
Very well explained. Helped me a lot, thank you!
Thanks. Keep Watching.
I would have visited this channel before I wasted my interview preparation time by looking ambiguous lectures from other sources, as I am not getting the core of DP, Greedy, recursion etc..
Thanks for nice words. Glad you liked content & channel. Keep Watching.
Thanks for the great explanation
Thanks for your nice feedback. Keep Watching.
The video was very helpful, thank you :)
Thanks for your nice feedback. Keep Watching.
Great explanation!!
Thanks for your nice feedback. Keep Watching.
reverse the string and find lcs with original string..how does this soln work??
Thank uu sir for this solution
Thanks Adarsh for your feedback. Keep Watching.
Well explained sir, ur great sir
Thanks for your nice feedback. Keep Watching.
Excellent, i like the explanation and different approaches. However, how do you print the string efficiently? I see alot of videos miss out on this part.
Hi, I didn't get the question. Which String you're mentioning here. Could you please elaborate it, so that I can check it.
@@CodingSimplified how do you print the solution in string rather than returning just the length.
If longest palindromic subsequence is aba,
How do we print that string from the table rather than returning just 3.
www.geeksforgeeks.org/print-longest-palindromic-subsequence/ you can see that here
why you add +2 if they are equal on bottum up approach
for string abba
longest substring is 4
but using this approach we initialize to 1 and coninue so adding 2 to this will.not give 4
Could you reply on this
Sir why are we beginning the loop in bottom-up approach from end i didn't get that part
You can even start from top, if you start from top, it'll fill left diagonal side. There's no specific reason. Just that we want to cover basic to complete string, so any one of diagonal you need to fill. Left or right diagonal side.
String is not printing how to print palindrome
string?
class Solution {
public String solve(String finalstr,int start,int end,String dp[][]){
if(start==end)
return String.valueOf(finalstr.charAt(start));
if(start>end)
return "";
if(dp[start][end]!=null)
return dp[start][end];
if(finalstr.charAt(start)==finalstr.charAt(end))
return finalstr.charAt(start)+solve(finalstr,start+1,end-1,dp)+finalstr.charAt(end);
String retvalue1 = solve(finalstr,start+1,end,dp);
String retvalue2 = solve(finalstr,start,end-1,dp);
if(retvalue1.length()>retvalue2.length()){
dp[start][end] = retvalue1;
return retvalue1;
}
else{
dp[start][end] = retvalue2;
return retvalue2;
}
}
public int longestPalindromeSubseq(String s) {
String dp[][] = new String[s.length()+1][s.length()+1];
String ans = solve(s,0,s.length()-1,dp);
return ans.length();
}
}
What is DP sir please reply
DP - Dynamic Programming is technique to solve coding problems.