[LeetCode]139.Word Break

题目

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”.

思路

递归

代码

/*---------------------------------------
*   日期:2015-05-07
*   作者:SJF0115
*   题目: 139.Word Break
*   网址:https://leetcode.com/problems/word-break/
*   结果:AC
*   来源:LeetCode
*   博客:
-----------------------------------------*/
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;

class Solution {
public:
    bool wordBreak(string s, unordered_set<string>& wordDict) {
        int size = wordDict.size();
        if(size == 0){
            return false;
        }//if
        vector<bool> visited(s.size(),false);
        return helper(s,wordDict,0,visited);
    }
private:
    bool helper(string &s,unordered_set<string>& wordDict,int index,vector<bool> &visited){
        if(index >= s.size()){
            return true;
        }//if
        // 已经查看过表示行不通
        if(visited[index]){
            return false;
        }//if
        visited[index] = true;
        // 以index下标开始
        for(int i = index;i < s.size();++i){
            if(wordDict.find(s.substr(index,i - index + 1)) != wordDict.end()){
                if(helper(s,wordDict,i+1,visited)){
                    return true;
                }//if
            }//if
        }//for
        return false;
    }
};

int main() {
    Solution solution;
    string str("leetcode");
    unordered_set<string> wordDict = {"leet","co","code"};
    cout<<solution.wordBreak(str,wordDict)<<endl;
}

运行时间

时间: 2024-11-01 21:20:32

[LeetCode]139.Word Break的相关文章

【LeetCode从零单排】No198.House Robber &amp;amp;&amp;amp;No91.Decode Ways&amp;amp;&amp;amp;139 word break(动态规划典型应用)

1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security

前端-word wrap和word break为啥设计成一个属性?

问题描述 word wrap和word break为啥设计成一个属性? word-wrap: The word-wrap property is used to specify whether or not the browser may break lines within words in order to prevent overflow when an otherwise unbreakable string is too long to fit in its containing bo

Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

[LeetCode] Longest Word in Dictionary 字典中的最长单词

Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest l

[LeetCode]127.Word Ladder

题目 Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given: s

[LeetCode]79.Word Search

[题目] Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not

Word Break

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]--290. Word Pattern

Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str. Examples: pattern = "abba", str = "dog cat cat d

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能