Skip to content

Commit

Permalink
Time: 5 ms (79.15%), Space: 8.2 MB (77.64%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed May 5, 2023
1 parent d71f64d commit 93d06e7
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions 0139-word-break/0139-word-break.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
public:
int ans=-2;

bool solve(string &s,int i, unordered_map<string,int> &mp,vector<int> &dp )
{
if(i>=s.size() )return true;


if(dp[i]!=-1)return dp[i];
string str;
for(int j=i;j<s.size();j++)
{
str+=s[j];
if(mp.find(str)!=mp.end())
{
if(solve(s,j+1,mp,dp)) return dp[i]=true;

}
}
return dp[i]=false;
}
bool wordBreak(string s, vector<string>& word) {
unordered_map<string,int> mp;
for(int i=0;i<word.size();i++)
{
mp[word[i]]++;
}
vector<int> dp(301,-1);
string str="";
return solve(s,0,mp,dp);

}
};

0 comments on commit 93d06e7

Please sign in to comment.