vector<string> wordBreak(string s, vector<string>& wordDict) { if (!wordBreak2(s, wordDict))return{}; vector<vector<string>> dp(s.size() + 1, vector<string>()); for (int i = 0; i < s.length(); i++) { if (i&&dp[i].empty())continue; for (string word : wordDict) { int wlen = word.length(); int newEnd = i + wlen; if (newEnd > s.length())continue; if (s.compare(i, wlen, word, 0, wlen))continue; if (i == 0) { dp[newEnd].push_back(word); continue; } for (string d : dp[i]) dp[newEnd].push_back(d + " " + word); } } return dp.back(); }