nikunjsoni

139

Jun 24th, 2021
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     bool wordBreak(string s, vector<string>& wordDict) {
  4.         set<string> word_set(wordDict.begin(), wordDict.end());
  5.         vector<bool> dp(s.length() + 1);
  6.         dp[0] = true;
  7.  
  8.         for(int i = 1; i <= s.length(); i++) {
  9.             for(int j = 0; j < i; j++) {
  10.                 if(dp[j] && word_set.find(s.substr(j, i - j)) != word_set.end()) {
  11.                     dp[i] = true;
  12.                     break;
  13.                 }
  14.             }
  15.         }
  16.         return dp[s.length()];
  17.     }
  18. };
Advertisement
Add Comment
Please, Sign In to add comment