leetcode笔记: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".

二. 题目分析

假设使用递归,会超时。

这时使用动态规划就可以解决这个问题,即将源字符串s从開始到结尾。分解成各个子串进行操作,对于这类字符串组合问题,须要掌握相似状态转移方程。

对于下标i所相应字符的匹配状态flag[i],假设dict有字符串能够匹配,这取决于之前某个字符j的状态出现匹配。从数组s的j + 1i下标之间的字符也能从dict中找到匹配的字符串:

flag[i] = any(flag[j] && (s[j + 1, i] ∈ dict))

三. 演示样例代码

class Solution
{
public:
    bool wordBreak(string s, unordered_set<string> &dict)
    {
        vector<bool> wordFlag(s.size() + 1, false); // 动态规划
        wordFlag[0] = true;
        for (int i = 1; i < s.size() + 1; ++i)
        {
            for (int j = i - 1; j >= 0; --j)
            {
                if (wordFlag[j] && dict.find(s.substr(j, i - j)) != dict.end())
                {
                    wordFlag[i] = true;
                    break;
                }
            }
        }
        return wordFlag[s.size()];
    }
};

四. 小结

动态规划对于解决一些字符串的问题也是有效且easy实现的。

原文地址:https://www.cnblogs.com/llguanli/p/8334015.html

时间: 2024-11-09 00:03:12

leetcode笔记:Word Break的相关文章

【leetcode】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】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 &

[C++]LeetCode: 113 Word Break II (DP &amp;&amp; Backtacking) 求解拆分组合

题目: 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, given s = "catsanddog", dict = ["cat", "cats

leetcode[140] Word Break II

这题是上一题的加强版,这里需要返回的是所有可能的分割词.例如: s = "catsanddog",dict = ["cat", "cats", "and", "sand", "dog"]. A solution is ["cats and dog", "cat sand dog"]. 先用dp求得每个起点到终点是否符合word break 1中的条

Leetcode dp Word Break

Word Break Total Accepted: 22281 Total Submissions: 105657My Submissions 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 = "leetcod

leetcode之Word Break

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 t

【leetcode】Word Break (middle)

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 dfs Word Break II

Word Break II Total Accepted: 15138 Total Submissions: 92228My Submissions 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 ex

【LeetCode】Word Break II 动态规划

题目:Word Break 要求找到所有能够有字典中的词重组成目标串的结果 public class Solution { public static List<String> wordBreak(String s, Set<String> dict) { List<String> dp[] = new ArrayList[s.length()+1]; dp[0] = new ArrayList<String>(); for(int i=0; i<s.

【LeetCode】Word Break 动态规划

题目:Word Break 思路:将一个串可以划分的共有s.length+1个点,判断长为n的串是否能由字典中的词组成,则看之前有没有划分点能使其处于字典中 ,这样该问题 就分解为子问题的求解 所以可以使用动态规划 <span style="font-size:18px;">public class Solution { public boolean wordBreak(String s, Set<String> dict) { boolean[] tag =