knakul853

Untitled

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