Challenge - 5 Problems
Word Break Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Word Break DP Table
What is the output of the DP table after running the word break algorithm on the string
"leetcode" with dictionary {"leet", "code"}?DSA C++
bool wordBreak(string s, vector<string>& wordDict) { unordered_set<string> dict(wordDict.begin(), wordDict.end()); vector<bool> dp(s.size() + 1, false); dp[0] = true; for (int i = 1; i <= s.size(); i++) { for (int j = 0; j < i; j++) { if (dp[j] && dict.count(s.substr(j, i - j))) { dp[i] = true; break; } } } for (bool val : dp) cout << (val ? 1 : 0) << " "; return dp[s.size()]; } // Called with s = "leetcode", wordDict = {"leet", "code"}
Attempts:
2 left
💡 Hint
Trace the DP array from start to end, marking positions where substrings are found in the dictionary.
✗ Incorrect
The DP array starts with dp[0] = true. 'leet' is found from index 0 to 4, so dp[4] = true. Then 'code' is found from index 4 to 8, so dp[8] = true. The rest remain false.
🧠 Conceptual
intermediateUnderstanding Word Break Problem Complexity
What is the time complexity of the classic dynamic programming solution to the Word Break problem for a string of length n and a dictionary of size m?
Attempts:
2 left
💡 Hint
Consider the nested loops and substring checks involved in the DP solution.
✗ Incorrect
The DP solution uses two nested loops over the string length n, and for each substring, it checks membership in the dictionary which can be O(m) if implemented naively.
🔧 Debug
advancedIdentify the Bug in Word Break Implementation
Given the following code snippet, what error will it cause when run with s = "applepenapple" and wordDict = {"apple", "pen"}?
bool wordBreak(string s, vector& wordDict) {
unordered_set dict(wordDict.begin(), wordDict.end());
vector dp(s.size(), false);
dp[0] = true;
for (int i = 1; i <= s.size(); i++) {
for (int j = 0; j < i; j++) {
if (dp[j] && dict.count(s.substr(j, i - j))) {
dp[i] = true;
break;
}
}
}
return dp[s.size()];
}
Attempts:
2 left
💡 Hint
Check the size of the dp vector and the indices accessed.
✗ Incorrect
The dp vector is initialized with size s.size(), but the code accesses dp[s.size()] which is out of range causing runtime error.
🚀 Application
advancedResulting Segmentation Count
How many different ways can the string
"catsanddog" be segmented into dictionary words from {"cat", "cats", "and", "sand", "dog"}?Attempts:
2 left
💡 Hint
Try to find all valid segmentations by splitting the string at dictionary words.
✗ Incorrect
The two segmentations are "cats and dog" and "cat sand dog".
❓ Predict Output
expertOutput of Word Break with Overlapping Dictionary Words
What is the output of the following code snippet?
string s = "pineapplepenapple";
vector wordDict = {"apple", "pen", "applepen", "pine", "pineapple"};
bool wordBreak(string s, vector& wordDict) {
unordered_set dict(wordDict.begin(), wordDict.end());
vector dp(s.size() + 1, false);
dp[0] = true;
for (int i = 1; i <= s.size(); i++) {
for (int j = 0; j < i; j++) {
if (dp[j] && dict.count(s.substr(j, i - j))) {
dp[i] = true;
break;
}
}
}
return dp[s.size()];
}
cout << (wordBreak(s, wordDict) ? "true" : "false") << endl;
Attempts:
2 left
💡 Hint
Consider that multiple dictionary words can overlap and still allow full segmentation.
✗ Incorrect
The string can be segmented as "pine apple pen apple", "pineapple pen apple", or "pine applepen apple", so the function returns true.
