knakul853

Untitled

Jul 17th, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     bool wordBreak(string s, vector<string>& wordDict) {
  4.        
  5.         int n = s.size();
  6.         bool dp[n+1]={0};
  7.        
  8.         set<string> st(wordDict.begin(), wordDict.end());
  9.         dp[0] = true;
  10.        
  11.         for(int i=1;i<=n;i++)
  12.         {
  13.            
  14.             for(int j=0;j<i;j++)
  15.             {
  16.                 string check = s.substr(j, i-j);
  17.                 if(dp[j] && (st.find(check)!=st.end()))
  18.                 {
  19.                     dp[i] = true;
  20.                     break;
  21.                 }
  22.             }
  23.         }
  24.        
  25.         return dp[n];
  26.        
  27.     }
  28. };
Add Comment
Please, Sign In to add comment