Given a string s and a dictionary of words dict, determine if s can be segmented into
a space-separated sequence of one or more dictionary words.
For example, given
s = "leetcode",
dict = ["leet", "code"].
Return true because "leetcode" can be segmented as "leet code".
Solution: dp.
1 class Solution { 2 public: 3 4 bool wordBreak(string s, unordered_set<string> &dict) { 5 int N = s.length(); 6 vector<bool> canBreak(N+1,false); 7 canBreak[0] = true; 8 for (int i = 1; i <= N; i++) { 9 for (int j = i-1; j >= 0; j--) { 10 if (canBreak[j] && dict.find(s.substr(j,i-j)) != dict.end()) { 11 canBreak[i] = true; 12 break; 13 } 14 } 15 } 16 return canBreak[N]; 17 } 18 };