Given a string s and a dictionary of words dict, determine if s can be break into a space-separated sequence of one or more dictionary words.
Example
Given s = "lintcode"
, dict = ["lint", "code"]
.
Return true because "lintcode" can be break as "lint code"
.
memory超出限制的算法:
class Solution {
public:
/**
* @param s: A string s
* @param dict: A dictionary of words dict
*/
bool wordSegmentation(string s, unordered_set<string> &dict) {
// write your code here
if (s.size() == 0)
return true;
for (int i=0; i<s.size(); i++)
{
string curString = s.substr(0, i+1);
if (dict.count(curString) > 0)
{
if (wordSegmentation(s.substr(i+1, s.size()-curString.size()), dict))
return true;
}
}
return false;
}
};