class Solution { public: bool wordBreak(string s, vector<string>& wordDict) { int n = s.length(); if (n == 0) return false; unordered_set<string> wordSet(wordDict.begin(), wordDict.end()); vector<bool> dp(n+1, false); dp[0] = true; for (int i = 0; i < n; i++) for (int j = i; j >= 0; --j) if (dp[j] && wordSet.count(s.substr(j, i-j+1))) { dp[i+1] = true; break; } return dp[n]; } };