Advertisement
sak1b

Untitled

Nov 18th, 2021
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1.  
  2. //Approach-1: Memoization DP
  3. class Solution {
  4. int dp[305];
  5. unordered_set<string> st;//dictionary
  6. int fun(int i, string s){
  7. if(i==s.size()) return true;
  8. if(dp[i]!=-1) return dp[i];
  9.  
  10. for(int j=i;j<s.size();j++)
  11. if(st.count(s.substr(i,j-i+1)) && fun(j+1,s))
  12. return dp[i] = 1;
  13. return dp[i] = 0;
  14. }
  15. public:
  16. bool wordBreak(string s, vector<string>& wordDict) {
  17. memset(dp,-1,sizeof(dp));
  18. for(string& x: wordDict)
  19. st.insert(x);
  20.  
  21. return fun(0,s)==1?true:false;
  22. }
  23. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement