LeetCode #139. Word Break C#

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

Solution:

Need to use DP because derictly loop through the string may not return the correct answer.

Test case: s="aaaaaaa", dict = ["aaa", "aaaa"]

Two solutions:

First Dynamic programming:

dp[0] = true;// always true, because empty string can always add space to it.

dp[1] = dp[0]&&dict.Contains(s.Substring(0, 1);

dp[2] =( dp[1]&&dict.Contains(s.Substring(1,1))) ||(dp[0]&& dict.Contains(s.Substring(0,2)));

so need to loop through dp[j]&&wordDict.Contains(s.Substring(j,i-j)) untill dp[i] is true where j from 0 to i;

 1 public class Solution {
 2     public bool WordBreak(string s, ISet<string> wordDict)
 3         {
 4             ISet<int> set = new HashSet<int>();
 5             if(string.IsNullOrEmpty(s))
 6             {
 7                 return true;
 8             }
 9             int l = s.Length;
10             bool[] dp = new bool[l+1];
11             dp[0] = true;
12             for(int i=1; i<l+1; i++)
13             {
14                 for(int j=0; j<i; j++)
15                 {
16                     dp[i]=dp[j]&&wordDict.Contains(s.Substring(j,i-j));
17                     if(dp[i])
18                     {
19                         break;
20                     }
21                 }
22             }
23             return dp[l];
24         }
25
26 }

Second DFS:

Use recursion in Dfs to mark the indexes those already looped through and returned false;

 1 public class Solution {
 2     public bool WordBreak(string s, ISet<string> wordDict)
 3         {
 4             ISet<int> set = new HashSet<int>();
 5             return Dfs(s, 0, wordDict, set);
 6
 7         }
 8         private bool Dfs(string s, int index, ISet<string> dict, ISet<int> set)
 9         {
10             // base case
11             if (index == s.Length) return true;
12             // check memory
13             if (set.Contains(index)) return false;
14             // recursion
15             for (int i = index + 1; i <= s.Length; i++)
16             {
17                 String t = s.Substring(index, i-index);
18                 if (dict.Contains(t))
19                     if (Dfs(s, i, dict, set))
20                         return true;
21                     else
22                         set.Add(i);
23             }
24             set.Add(index);
25             return false;
26         }
27 }

  

时间: 2024-08-10 19:01:38

LeetCode #139. Word Break C#的相关文章

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, givens = "leetcode",dict = ["leet", "code"]. Return true because &

leetcode 139 word break (单词拆分)

一开始的错误答案与错误思路,幻想直接遍历得出答案: 1 class Solution { 2 public: 3 bool wordBreak(string s, vector<string>& wordDict) { 4 for(int i;i<s.size();i++){ 5 int step=0; 6 for(int j;j<wordDict.size();j++){ 7 if(s.substr(i,wordDict[j].size())==wordDict[j]){

Java for 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

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

【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