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
What is the output of the following code that checks if the string can be segmented into dictionary words?
DSA Python
def word_break(s, word_dict): dp = [False] * (len(s) + 1) dp[0] = True for i in range(1, len(s) + 1): for j in range(i): if dp[j] and s[j:i] in word_dict: dp[i] = True break return dp[-1] s = "applepenapple" word_dict = {"apple", "pen"} print(word_break(s, word_dict))
Attempts:
2 left
💡 Hint
Think about whether the string can be split into the words 'apple' and 'pen'.
✗ Incorrect
The string 'applepenapple' can be segmented as 'apple' + 'pen' + 'apple', all present in the dictionary, so the output is True.
❓ Predict Output
intermediateOutput of Word Break with No Possible Segmentation
What is the output of this code when the string cannot be segmented into dictionary words?
DSA Python
def word_break(s, word_dict): dp = [False] * (len(s) + 1) dp[0] = True for i in range(1, len(s) + 1): for j in range(i): if dp[j] and s[j:i] in word_dict: dp[i] = True break return dp[-1] s = "catsandog" word_dict = {"cats", "dog", "sand", "and", "cat"} print(word_break(s, word_dict))
Attempts:
2 left
💡 Hint
Check if the string can be split into the given dictionary words.
✗ Incorrect
The string 'catsandog' cannot be segmented into the dictionary words because 'og' is not in the dictionary, so the output is False.
🧠 Conceptual
advancedUnderstanding the DP Array in Word Break
In the Word Break problem, what does the boolean array dp represent after the algorithm finishes?
Attempts:
2 left
💡 Hint
Think about what the index i in dp corresponds to in the string.
✗ Incorrect
dp[i] indicates whether the substring from the start up to index i (exclusive) can be segmented into words from the dictionary.
🔧 Debug
advancedIdentify the Error in Word Break Implementation
What error will this code raise when executed?
DSA Python
def word_break(s, word_dict): dp = [False] * len(s) dp[0] = True for i in range(1, len(s) + 1): for j in range(i): if dp[j] and s[j:i] in word_dict: dp[i] = True break return dp[-1] s = "leetcode" word_dict = {"leet", "code"} print(word_break(s, word_dict))
Attempts:
2 left
💡 Hint
Check the size of dp and the indices used to access it.
✗ Incorrect
The dp array is initialized with length len(s), but the code accesses dp[i] where i can be len(s), causing IndexError.
🚀 Application
expertNumber of Ways to Segment String Using Word Break
Given the string and dictionary below, what is the number of ways to segment the string into dictionary words?
DSA Python
def word_break_count(s, word_dict): dp = [0] * (len(s) + 1) dp[0] = 1 for i in range(1, len(s) + 1): for j in range(i): if s[j:i] in word_dict: dp[i] += dp[j] return dp[-1] s = "catsanddog" word_dict = {"cat", "cats", "and", "sand", "dog"} print(word_break_count(s, word_dict))
Attempts:
2 left
💡 Hint
Count all possible segmentations like 'cats and dog' and 'cat sand dog'.
✗ Incorrect
There are exactly two ways to segment 'catsanddog': 'cats and dog' and 'cat sand dog'.
