From 793f2fc69b807cac2d010275fc88fcefb83a844a Mon Sep 17 00:00:00 2001 From: Ravi Maurya <39462517+raviMaurya12@users.noreply.github.com> Date: Fri, 2 Oct 2020 13:43:24 +0530 Subject: [PATCH] added word break solution --- LeetCode/word_break.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 LeetCode/word_break.cpp 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