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:
Matej Focko 2025-03-20 17:59:18 +01:00
parent ba5a9515ba
commit 0f79fcd5ed
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

18
cs/word-break.cs Normal file
View 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];
}
}