leetcode || 139、Word Break

problem:

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

Hide Tags

Dynamic Programming

thinking:

从s的第一个字母向后匹配,如果i前面的前缀可以匹配,就看s字符串i以后的后缀是否匹配

code:

class Solution{
    public:
bool wordBreakHelper(string s, unordered_set<string> &dict,set<string> &unmatch) {
        if(s.length() < 1) return true;
		bool flag = false;
		for(int i = 1; i <= s.length(); i++)
		{
			string prefixstr = s.substr(0,i);
			unordered_set<string>::iterator it = dict.find(prefixstr);
			if(it != dict.end())
			{
				string suffixstr = s.substr(i);
				set<string>::iterator its = unmatch.find(suffixstr);
				if(its != unmatch.end())continue;
				else{
					flag = wordBreakHelper(suffixstr,dict,unmatch);
					if(flag) return true;
					else unmatch.insert(suffixstr);
				}
			}
		}
		return false;
    }
	bool wordBreak(string s, unordered_set<string> &dict) {
        int len = s.length();
		if(len < 1) return true;
		set<string> unmatch;
		return wordBreakHelper(s,dict,unmatch);
    }
};
时间: 2024-10-22 13:11:29

leetcode || 139、Word Break的相关文章

leetcode || 140、Word Break II

problem: 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", "

[email&#160;protected] [139/140] Word Break &amp; Word Break II

https://leetcode.com/problems/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"

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 tr

leetcode || 79、Word Search

problem: 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

LeetCode: Word Break [139]

[题目] 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 be

LeetCode: Word Break II 解题报告

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

139. Word Break(js)

139. Word Break Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. Note: The same word in the dictionary may be r

[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中的条