Thanks for the video Aditi, because of your video I realised why it's a dp problem and more importantly how we can optimize it. 1-D dp solution [C++] [Memoization] class Solution{ private: bool solve(int ind,string &s,int str_len,unordered_map &mp,vector &dp){ if(ind==str_len) return true;
class Solution { public boolean wordBreak(String s, List wordDict) { int [][]dp= new int [s.length()][s.length()]; for(int i=0;i= s.length()) { return false; } if(dp[start][end]!=-1){ return dp[start][end]==1; }
why dont we take "wordDict" and traverse them to from "string s" like leet+code = leetcode, apple+dogs+apple =appledogapple, since this was in recursion section i avoided going in DP here. droping the code here, review it as it is giving TLE after 32/46 testcases class Solution { private: bool flag = false; void solve(vector& wordDict, int index, string st, string temp){ // cout
Hey! I am in my third year and still don't have any idea about coding. I am thinking of doing DSA with c++ but don't know where and how to start. Also, striver's DSA playlist is not complete. Can you please guide me? I am planning and trying to get off-campus placement in a product-based company.
Thanks for the video Aditi, because of your video I realised why it's a dp problem and more importantly how we can optimize it.
1-D dp solution [C++] [Memoization]
class Solution{
private:
bool solve(int ind,string &s,int str_len,unordered_map &mp,vector &dp){
if(ind==str_len)
return true;
if(dp[ind]!=-1)
return dp[ind];
for(int i=ind;i
great work !
hello Aditi, here is c++ code with 2D dp
note: s.substr(start,length): this function take length instead of end in c++,
class Solution {
public:
bool solve(int start ,int end,string &s,set &wD,vector &dp){
if(dp[start][end]!=-1){
return dp[start][end]==1 ?true:false;
}
if(end==s.length()-1){
if(wD.contains(s.substr(start,end - start + 1))){
dp[start][end] = 1;
return true;
}
return false;
}
if(wD.contains(s.substr(start,end - start + 1))){
if(solve(end+1,end+1,s,wD,dp)) {
dp[start][end]=1;
return true;
}
}
bool ans=solve(start,end+1,s,wD,dp);
dp[start][end]= ans ? 1:0;
return ans;
}
bool wordBreak(string s, vector& wordDict) {
setwd(wordDict.begin(), wordDict.end());
vector dp(s.length(), vector(s.length(), -1));
return solve(0,0,s,wd,dp);
}
};
make more vdios youur explanation is amazing
Thank you, I will
one side of tree(0,6) returns false and other (5,9) returns true , which one will be considered
class Solution {
public boolean wordBreak(String s, List wordDict) {
int [][]dp= new int [s.length()][s.length()];
for(int i=0;i= s.length()) {
return false;
}
if(dp[start][end]!=-1){
return dp[start][end]==1;
}
if(wd.contains(s.substring(start, end+1))){
if(end==s.length()-1 || fns(end+1, end+1, s, wd,dp)){
dp[start][end]=1;
return true;
}
}
boolean ans=fns(start, end+1, s, wd,dp);
dp[start][end]=ans? 1:0;
return ans;
}
}
Amazing solution , Thanks for making this video
Hey Aditi I am getting problem in this, because public function is taking vector as argument , and we are giving 'set' in the private function
Yes, please create a set and add all the values of this vector to the set.
@@algorithmsbyaditi Bro, I have just sent you a connection request, so that i can ask my doubt there if would help me.
why dont we take "wordDict" and traverse them to from "string s" like leet+code = leetcode, apple+dogs+apple =appledogapple, since this was in recursion section i avoided going in DP here.
droping the code here, review it as it is giving TLE after 32/46 testcases
class Solution {
private:
bool flag = false;
void solve(vector& wordDict, int index, string st, string temp){
// cout
I have gone through several videos about this problem. But I didn't find the solution that you explained. Great video :)
Glad it helped!
@@algorithmsbyaditi Can you share your approach for Longest Palindrome Substring?? With the similar approach !!!
Thank you
I solved that using two pointers. For solution please reach out on aditichourasia10@gmail.com
Hey!
I am in my third year and still don't have any idea about coding.
I am thinking of doing DSA with c++ but don't know where and how to start. Also, striver's DSA playlist is not complete.
Can you please guide me?
I am planning and trying to get off-campus placement in a product-based company.
Yes, you can connect with me on LinkedIn(Aditi Chourasia). I will surely try to guide you with the best of my knowledge.