Challenge - 5 Problems
Word Break Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Word Break Check for Simple Case
What is the output of the following JavaScript code that checks if the string can be segmented into dictionary words?
DSA Javascript
function wordBreak(s, wordDict) {
const dp = Array(s.length + 1).fill(false);
dp[0] = true;
for (let i = 1; i <= s.length; i++) {
for (let j = 0; j < i; j++) {
if (dp[j] && wordDict.includes(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
return dp[s.length];
}
console.log(wordBreak("leetcode", ["leet", "code"]));Attempts:
2 left
💡 Hint
Think about whether the string "leetcode" can be split into words from the dictionary ["leet", "code"].
✗ Incorrect
The string "leetcode" can be split as "leet" + "code", both are in the dictionary, so the function returns true.
❓ Predict Output
intermediateOutput for Word Break with Valid Segmentation
What does the following code print for the given string and dictionary?
DSA Javascript
function wordBreak(s, wordDict) {
const dp = Array(s.length + 1).fill(false);
dp[0] = true;
for (let i = 1; i <= s.length; i++) {
for (let j = 0; j < i; j++) {
if (dp[j] && wordDict.includes(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
return dp[s.length];
}
console.log(wordBreak("applepenapple", ["apple", "pen", "pine"]));Attempts:
2 left
💡 Hint
Check if the string "applepenapple" can be segmented using the dictionary words.
✗ Incorrect
The string "applepenapple" can be segmented as "apple" + "pen" + "apple", all in the dictionary, so the output is true.
❓ Predict Output
advancedOutput for Word Break with Overlapping Dictionary Words
What is the output of this code when the dictionary contains overlapping words?
DSA Javascript
function wordBreak(s, wordDict) {
const dp = Array(s.length + 1).fill(false);
dp[0] = true;
for (let i = 1; i <= s.length; i++) {
for (let j = 0; j < i; j++) {
if (dp[j] && wordDict.includes(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
return dp[s.length];
}
console.log(wordBreak("catsanddog", ["cat", "cats", "and", "sand", "dog"]));Attempts:
2 left
💡 Hint
Try to segment "catsanddog" using the dictionary words, considering overlapping words like "cat" and "cats".
✗ Incorrect
The string can be segmented as "cats" + "and" + "dog" or "cat" + "sand" + "dog", so the output is true.
❓ Predict Output
advancedOutput for Word Break with Empty String
What does the code output when the input string is empty?
DSA Javascript
function wordBreak(s, wordDict) {
const dp = Array(s.length + 1).fill(false);
dp[0] = true;
for (let i = 1; i <= s.length; i++) {
for (let j = 0; j < i; j++) {
if (dp[j] && wordDict.includes(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
return dp[s.length];
}
console.log(wordBreak("", ["a", "b"]));Attempts:
2 left
💡 Hint
Consider if an empty string can be segmented into dictionary words.
✗ Incorrect
An empty string is considered segmented by default, so the function returns true.
🧠 Conceptual
expertTime Complexity of Word Break Algorithm
What is the time complexity of the dynamic programming solution for the Word Break problem given a string of length n and a dictionary of size m?
Attempts:
2 left
💡 Hint
Consider the nested loops and dictionary lookup time when using a hash set.
✗ Incorrect
The algorithm uses two nested loops over the string length n, resulting in O(n^2). Dictionary lookups are O(1) if a hash set is used, so total time is O(n^2).
