class Solution {
public:
bool wordBreak(string s, unordered_set<string> &dict) {
int n = s.length();
if(n == 0)
return true;
bool * canBreak = new bool[n];
memset(canBreak,false, n*sizeof(bool));
for(int i = 0; i < n;i++)
{
if(dict.count(s.substr(0,i+1)) > 0)
{
canBreak[i] = true;
continue;
}
for(int j = 0; j < i;j++)
{
if(canBreak[j] && dict.count(s.substr(j+1,i-j))>0)
{
canBreak[i]=true;
break;
}
}
}
bool result = canBreak[n-1];
delete [] canBreak;
return result;
}
};
leetcode:Word Break
最新推荐文章于 2021-03-11 16:10:14 发布