Leetcode Blind 75 C++: Longest Common Subsequence

preview_player
Показать описание
Hey Everyone, Im going over the Blind 75, so decided to make a series on them to help solidify concepts prior to interviews. Below I have attached my notes from doing questions and if have any other questions feel free to let me know!

Thank you
Рекомендации по теме
Комментарии
Автор

//Input
/*
Given two strings text1 and text2
*/

//What DS/Algo/Technique
/*
Dynamic Programming to scan both text1 and 2 using a DP table, to check for character and size of our
text strings
*/

//What to do with the data
/*
Steps:
1. Initlize a DP table with the size of (text1.size()+1) x (text2.size()+1)
//This is just how we create our table with the two texts
2. Loop through each character in both strings
3. If the current characters in both strings are the same, the value of the current cell is 1 + the
value of the cell diagonially of the table both aboe and to the left of it
4. Otherwise, if they are different, teh value of hte current cell is the maximum between the cell to
the left and the cell above.
5. Return the value in the bottom right corner of the DP table we created to shwo the longest comon
subsequence
*/

//Output
/*
Return the length of their longest common subsequence
If there is no common subsequence, return 0
*/
class Solution {
public:
int text1, string text2) {
//Step 1
vector<vector<int>> dp(text1.size() + 1, vector<int>(text2.size() + 1));
//Step 2
for(int i = 1; i <= text1.size(); i++){
for(int j = 1; j <= text2.size(); j++){
//Step 3
if (text1[i - 1] == text2[j - 1]){
dp[i][j] = dp[i - 1][j - 1] + 1;
}else {
//Step 4
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
//Step 5
return
}
};

GChief
welcome to shbcf.ru