Lecture 135: Longest Common Subsequence || DP on Strings

preview_player
Показать описание
In this Video, we are going to learn about Dynamic Programming. This Video marks the start of India's Biggest DP Series.

There is a lot to learn, Keep in mind “ Mnn bhot karega k chor yrr apne se nahi hoga ya maza nahi aara, Just ask 1 question “ Why I started ? “

Questions Links:

Do provide you feedback in the comments, we are going to make it best collectively.

Connect with me here:

Telegram Group Link: Love Babbar CODE HELP

Intro Sequence: We have bought all the required Licenses of the Audio, Video & Animation used.

#DP_Series #LoveBabbar
Рекомендации по теме
Комментарии
Автор

U r genius Bhaiya. Hats off. GOD of DSA.

praveennemagoudar
Автор

Sir apka ye series maine kuch hi din pehle start kiya hai .
You are providing an amazing content which makes it easy for anyone to understand computer programming.
You are doing an amazing job!! Thank you sir!! @LoveBabbar

manishdv
Автор

// COMPLETE CODE WITH COMMENTS

class Solution {
public:

int solve(string& a, string& b, int i, int j){

// Base Case
if(i == a.length())
return 0;

if(j == b.length())
return 0;

int ans = 0;

if(a[i] == b[j])
ans = 1 + solve(a, b, i+1, j+1);

else
{
ans = max(solve(a, b, i+1, j), solve(a, b, i, j+1));
}

return ans;
}

int solveMem(string& a, string& b, int i, int j, vector<vector<int>> &dp){

// Base Case
if(i == a.length())
return 0;

if(j == b.length())
return 0;

if(dp[i][j] != -1)
return dp[i][j];

int ans = 0;

if(a[i] == b[j])
ans = 1 + solveMem(a, b, i+1, j+1, dp);

else
{
ans = max(solveMem(a, b, i+1, j, dp), solveMem(a, b, i, j+1, dp));
}

return dp[i][j] = ans;
}

int solveTab(string& a, string& b){

int n1 = a.length();
int n2 = b.length();

vector<vector<int>> dp(n1+1, vector<int> (n2+1, 0));

for(int i = n1-1; i>=0; i--){
for(int j = n2-1; j>=0; j--){
int ans = 0;

if(a[i] == b[j])
ans = 1 + dp[i+1][j+1];

else
{
ans = max(dp[i+1][j], dp[i][j+1]);
}

dp[i][j] = ans;
}
}

return dp[0][0];
}

int solveSpaceOP(string& a, string& b){

int n1 = a.length();
int n2 = b.length();

vector<vector<int>> dp(n1+1, vector<int> (n2+1, 0));

vector<int> currRow(n2+1, 0);
vector<int> nextRow(n2+1, 0);

for(int i = n1-1; i>=0; i--){
for(int j = n2-1; j>=0; j--){
int ans = 0;

if(a[i] == b[j])
ans = 1 + nextRow[j+1];

else
{
ans = max(nextRow[j], currRow[j+1]);
}

currRow[j] = ans;
}

nextRow = currRow;
}

return nextRow[0];
}

int text1, string text2) {
// RECURSION
// return solve(text1, text2, 0, 0);

// RECURSION + MEMOIZATION
/*
int n1 = text1.length();
int n2 = text2.length();

vector<vector<int>> dp(n1+1, vector<int> (n2+1, -1));

return solveMem(text1, text2, 0, 0, dp);
*/

// TABULATION
// return solveTab(text1, text2);

// SPACE OPTIMIZATION
return solveSpaceOP(text1, text2);
}
};

vedantraut
Автор

Amazing video as always....Thankkk youuuu bhaiya😍

arvindmali
Автор

I really love to learn by you <- Babbar Bhaiya -> 😍❤

Vital_Truth
Автор

Thanks Babbar Sir Fire 🔥 back to finish Dp series

sukhjitsingh
Автор

Appreciate your work. Thank you so much.

Sambyal-ny
Автор

Sir aapne top down me jo space Kam karne k liye do row liya hai usme aapne bola unki size min(lengths of strings) but ye galat hai aapko max Lena padega

deepakumar
Автор

really helpful bhaiya this series is very popular in future .

Kinggaming-hpqp
Автор

Very nice explanation sir, thank you so much

nothinginsideme
Автор

Thank you bhaiya for such amazing content.☺

Deepak-umzq
Автор

I have noticed that some of the tutors use memoization with bottom up and some with top down and vice versa

Here.s_how
Автор

can this ques be solved using 4 variables only?

BombSquadHindiTipsTricks
Автор

Bhaiya #QnA Why didn't you go to foreign country for job purpose or to stay there itself ?

GauravHaritas