diff --git a/LeetCode/word_break.cpp b/LeetCode/word_break.cpp new file mode 100644 index 0000000..84a0117 --- /dev/null +++ b/LeetCode/word_break.cpp @@ -0,0 +1,28 @@ +// Word break memoization solution + +class Solution { +public: + unordered_map mpp; + int dp[501]; + + bool get(string s,int index){ + if(index==s.size())return true; + if(dp[index]!=-1)return dp[index]; + string tmp=""; + bool ok=false; + for(int i=index;i0){ + ok=(ok|get(s,i+1)); + } + } + return dp[index]=ok; + } + + bool wordBreak(string s, vector& wordDict) { + for(auto el:wordDict)mpp[el]++; + for(int i=0;i<501;i++)dp[i]=-1; + bool ans=get(s,0); + return ans; + } +}; \ No newline at end of file