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, given
s = "leetcode",
dict = ["leet", "code"].
Return true because "leetcode" can be segmented as "leet code".
Have you been asked this question in an interview?
此题相当简单,动态规划
- bool wordBreak(string s, unordered_set<string> &dict) {
- int len=s.length();
- vector<bool> dp(len+1,false);
- dp[0]=true;
- for(int i=0;i<=len;i++){
- for(int j=0;j<i;j++){
- if(dict.find(s.substr(j,i-j))!=dict.end() && dp[j]==true){
- dp[i]=true;
- }
- }
- }
- return dp[len];
- }
相关热门文章
给主人留下些什么吧!~~
评论热议