cs: add «139. Word Break»
URL: https://leetcode.com/problems/word-break/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
ba5a9515ba
commit
0f79fcd5ed
1 changed files with 18 additions and 0 deletions
18
cs/word-break.cs
Normal file
18
cs/word-break.cs
Normal file
|
@ -0,0 +1,18 @@
|
|||
public class Solution {
|
||||
public bool WordBreak(string s, IList<string> wordDict) {
|
||||
var words = new HashSet<string>(wordDict);
|
||||
var dp = new bool[s.Length + 1];
|
||||
|
||||
dp[0] = true;
|
||||
for (var i = 1; i <= s.Length; ++i) {
|
||||
for (var j = 0; j < i; ++j) {
|
||||
if (dp[j] && words.Contains(s.Substring(j, i - j))) {
|
||||
dp[i] = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dp[s.Length];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue