Leetcode 139. Word Break (Medium) (cpp)

来源:互联网 发布:vb的注释符号 编辑:程序博客网 时间:2024/05/19 07:08

Leetcode 139. Word Break (Medium) (cpp)

Difficulty: Medium


/*139. Word Break (Medium)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, givens = "leetcode",dict = ["leet", "code"].Return true because "leetcode" can be segmented as "leet code".*/class Solution {public:bool wordBreak(string s, unordered_set<string>& wordDict) {vector<bool> t(s.size() + 1, false);t[0] = true;for (int i = 1; i <= s.size(); i++) {for (int j = i - 1; j >= 0; j--) {if (t[j]) {if (wordDict.find(s.substr(j, i - j)) != wordDict.end()) {t[i] = true;break;}}}}return t.back();}};


0 0