bool wordBreak(string s, vector<string>& wordDict) {
function<bool(int)> can = [&](int i) -> bool {
if (i == (int)s.size()) return true;
for (const string& word : wordDict) {
if (s.compare(i, word.size(), word) == 0 && can(i + word.size())) return true;
}
return false;
};
return can(0);
}
public boolean wordBreak(String s, List<String> wordDict) {
return can(s, wordDict, 0);
}
private boolean can(String s, List<String> wordDict, int i) {
if (i == s.length()) return true;
for (String word : wordDict) {
if (s.startsWith(word, i) && can(s, wordDict, i + word.length())) return true;
}
return false;
}
def word_break(s, word_dict):
def can(i):
if i == len(s): return True
for word in word_dict:
if s.startswith(word, i) and can(i + len(word)): return True
return False
return can(0)
function wordBreak(s: string, wordDict: string[]): boolean {
const can = (i: number): boolean => {
if (i === s.length) return true;
for (const word of wordDict) {
if (s.startsWith(word, i) && can(i + word.length)) return true;
}
return false;
};
return can(0);
}
bool wordBreak(string s, vector<string>& wordDict) {
vector<int> memo(s.size() + 1, -1);
function<bool(int)> can = [&](int i) -> bool {
if (i == (int)s.size()) return true;
if (memo[i] != -1) return memo[i];
bool ok = false;
for (const string& word : wordDict) {
if (s.compare(i, word.size(), word) == 0 && can(i + word.size())) {
ok = true;
break;
}
}
memo[i] = ok;
return ok;
};
return can(0);
}
public boolean wordBreak(String s, List<String> wordDict) {
int[] memo = new int[s.length() + 1];
Arrays.fill(memo, -1);
return can(s, wordDict, memo, 0);
}
private boolean can(String s, List<String> wordDict, int[] memo, int i) {
if (i == s.length()) return true;
if (memo[i] != -1) return memo[i] == 1;
boolean ok = false;
for (String word : wordDict) {
if (s.startsWith(word, i) && can(s, wordDict, memo, i + word.length())) {
ok = true;
break;
}
}
memo[i] = ok ? 1 : 0;
return ok;
}
def word_break(s, word_dict):
memo = {}
def can(i):
if i == len(s): return True
if i in memo: return memo[i]
ok = False
for word in word_dict:
if s.startswith(word, i) and can(i + len(word)):
ok = True
break
memo[i] = ok
return ok
return can(0)
function wordBreak(s: string, wordDict: string[]): boolean {
const memo = new Array<boolean | undefined>(s.length + 1);
const can = (i: number): boolean => {
if (i === s.length) return true;
if (memo[i] !== undefined) return memo[i] as boolean;
let ok = false;
for (const word of wordDict) {
if (s.startsWith(word, i) && can(i + word.length)) {
ok = true;
break;
}
}
memo[i] = ok;
return ok;
};
return can(0);
}
bool wordBreak(string s, vector<string>& wordDict) {
int n = s.size();
unordered_set<string> words(wordDict.begin(), wordDict.end());
vector<bool> dp(n + 1, false);
dp[n] = true;
for (int i = n - 1; i >= 0; i--) {
for (int j = i + 1; j <= n; j++) {
if (dp[j] && words.count(s.substr(i, j - i))) {
dp[i] = true;
break;
}
}
}
return dp[0];
}
public boolean wordBreak(String s, List<String> wordDict) {
int n = s.length();
Set<String> words = new HashSet<>(wordDict);
boolean[] dp = new boolean[n + 1];
dp[n] = true;
for (int i = n - 1; i >= 0; i--) {
for (int j = i + 1; j <= n; j++) {
if (dp[j] && words.contains(s.substring(i, j))) {
dp[i] = true;
break;
}
}
}
return dp[0];
}
def word_break(s, word_dict):
n = len(s)
words = set(word_dict)
dp = [False] * (n + 1)
dp[n] = True
for i in range(n - 1, -1, -1):
for j in range(i + 1, n + 1):
if dp[j] and s[i:j] in words:
dp[i] = True
break
return dp[0]
function wordBreak(s: string, wordDict: string[]): boolean {
const n = s.length;
const words = new Set(wordDict);
const dp = new Array<boolean>(n + 1).fill(false);
dp[n] = true;
for (let i = n - 1; i >= 0; i--) {
for (let j = i + 1; j <= n; j++) {
if (dp[j] && words.has(s.slice(i, j))) {
dp[i] = true;
break;
}
}
}
return dp[0];
}
bool wordBreak(string s, vector<string>& wordDict) {
int n = s.size();
unordered_set<string> words(wordDict.begin(), wordDict.end());
int maxLen = 0;
for (const string& w : wordDict) maxLen = max(maxLen, (int)w.size());
vector<bool> dp(n + 1, false);
dp[n] = true;
for (int i = n - 1; i >= 0; i--) {
for (int j = i + 1; j <= min(n, i + maxLen); j++) {
if (dp[j] && words.count(s.substr(i, j - i))) {
dp[i] = true;
break;
}
}
}
return dp[0];
}
public boolean wordBreak(String s, List<String> wordDict) {
int n = s.length();
Set<String> words = new HashSet<>(wordDict);
int maxLen = 0;
for (String w : wordDict) maxLen = Math.max(maxLen, w.length());
boolean[] dp = new boolean[n + 1];
dp[n] = true;
for (int i = n - 1; i >= 0; i--) {
for (int j = i + 1; j <= Math.min(n, i + maxLen); j++) {
if (dp[j] && words.contains(s.substring(i, j))) {
dp[i] = true;
break;
}
}
}
return dp[0];
}
def word_break(s, word_dict):
n = len(s)
words = set(word_dict)
max_len = 0
for w in word_dict: max_len = max(max_len, len(w))
dp = [False] * (n + 1)
dp[n] = True
for i in range(n - 1, -1, -1):
for j in range(i + 1, min(n, i + max_len) + 1):
if dp[j] and s[i:j] in words:
dp[i] = True
break
return dp[0]
function wordBreak(s: string, wordDict: string[]): boolean {
const n = s.length;
const words = new Set(wordDict);
let maxLen = 0;
for (const w of wordDict) maxLen = Math.max(maxLen, w.length);
const dp = new Array<boolean>(n + 1).fill(false);
dp[n] = true;
for (let i = n - 1; i >= 0; i--) {
for (let j = i + 1; j <= Math.min(n, i + maxLen); j++) {
if (dp[j] && words.has(s.slice(i, j))) {
dp[i] = true;
break;
}
}
}
return dp[0];
}