LeetCode 139. 单词拆分
原题链接
中等
作者:
toFuture
,
2025-03-17 00:31:01
· 江苏
,
所有人可见
,
阅读 3
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
typedef unsigned long long ULL;
const int P=131;
unordered_set<ULL> hash;
for(auto& word:wordDict)
{
ULL h=0;
for(auto c:word)h=h*P+c;
hash.insert(h);
}
int n=s.size();
vector<bool> f(n+1);
f[0]=true;
s=' '+s;
for(int i=0;i<n;i++)
{
if(f[i])
{
ULL h=0;
for(int j=i+1;j<=n;j++)
{
h=h*P+s[j];
if(hash.count(h))f[j]=true;
}
}
}
return f[n];
}
};