Challenge - 5 Problems
Word Break Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Word Break Check with Simple Dictionary
What is the output of this C code that checks if the string "leetcode" can be segmented into dictionary words?
DSA C
#include <stdio.h> #include <string.h> #include <stdbool.h> bool wordBreak(char *s, char **wordDict, int wordDictSize) { int len = strlen(s); bool dp[len + 1]; dp[0] = true; for (int i = 1; i <= len; i++) { dp[i] = false; for (int j = 0; j < i; j++) { if (dp[j]) { for (int k = 0; k < wordDictSize; k++) { int wordLen = strlen(wordDict[k]); if (wordLen == i - j && strncmp(s + j, wordDict[k], wordLen) == 0) { dp[i] = true; break; } } } if (dp[i]) break; } } return dp[len]; } int main() { char *dict[] = {"leet", "code"}; char s[] = "leetcode"; if (wordBreak(s, dict, 2)) printf("true\n"); else printf("false\n"); return 0; }
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 segmented as "leet" + "code", both present in the dictionary, so the output is true.
❓ Predict Output
intermediate2:00remaining
Output when string cannot be segmented
What is the output of this C code that checks if the string "applepenapple" can be segmented into dictionary words {"apple", "pen", "pine"}?
DSA C
#include <stdio.h> #include <string.h> #include <stdbool.h> bool wordBreak(char *s, char **wordDict, int wordDictSize) { int len = strlen(s); bool dp[len + 1]; dp[0] = true; for (int i = 1; i <= len; i++) { dp[i] = false; for (int j = 0; j < i; j++) { if (dp[j]) { for (int k = 0; k < wordDictSize; k++) { int wordLen = strlen(wordDict[k]); if (wordLen == i - j && strncmp(s + j, wordDict[k], wordLen) == 0) { dp[i] = true; break; } } } if (dp[i]) break; } } return dp[len]; } int main() { char *dict[] = {"apple", "pen", "pine"}; char s[] = "applepenapple"; if (wordBreak(s, dict, 3)) printf("true\n"); else printf("false\n"); return 0; }
Attempts:
2 left
💡 Hint
Check if the string can be split into words from the dictionary.
✗ Incorrect
The string "applepenapple" can be segmented as "apple" + "pen" + "apple", all present in the dictionary, so the output is true.
❓ Predict Output
advanced2:00remaining
Output for string with no valid segmentation
What is the output of this C code that checks if the string "catsandog" can be segmented into dictionary words {"cats", "dog", "sand", "and", "cat"}?
DSA C
#include <stdio.h> #include <string.h> #include <stdbool.h> bool wordBreak(char *s, char **wordDict, int wordDictSize) { int len = strlen(s); bool dp[len + 1]; dp[0] = true; for (int i = 1; i <= len; i++) { dp[i] = false; for (int j = 0; j < i; j++) { if (dp[j]) { for (int k = 0; k < wordDictSize; k++) { int wordLen = strlen(wordDict[k]); if (wordLen == i - j && strncmp(s + j, wordDict[k], wordLen) == 0) { dp[i] = true; break; } } } if (dp[i]) break; } } return dp[len]; } int main() { char *dict[] = {"cats", "dog", "sand", "and", "cat"}; char s[] = "catsandog"; if (wordBreak(s, dict, 5)) printf("true\n"); else printf("false\n"); return 0; }
Attempts:
2 left
💡 Hint
Try to segment the string using the dictionary words.
✗ Incorrect
The string "catsandog" cannot be segmented fully into the dictionary words because "og" is not in the dictionary, so output is false.
🧠 Conceptual
advanced1:30remaining
Understanding the DP Array in Word Break
In the Word Break problem, what does the boolean array dp represent during the algorithm execution?
Attempts:
2 left
💡 Hint
Think about what the dp array tracks as the algorithm progresses from start to end.
✗ Incorrect
dp[i] is true if the substring from the start of the string up to index i-1 can be segmented into dictionary words.
🚀 Application
expert2:30remaining
Number of Ways to Segment String Using Word Break
Given the string "catsanddog" and dictionary {"cat", "cats", "and", "sand", "dog"}, how many different ways can the string be segmented into dictionary words?
Attempts:
2 left
💡 Hint
Try to find all possible segmentations of the string using the dictionary words.
✗ Incorrect
The string "catsanddog" can be segmented as "cats and dog" and "cat sand dog", so there are 2 ways.