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 with Simple Dictionary
What is the output of this Rust code that checks if the string "applepenapple" can be segmented into dictionary words?
DSA Rust
fn word_break(s: &str, word_dict: &[&str]) -> bool { let n = s.len(); let mut dp = vec![false; n + 1]; dp[0] = true; for i in 1..=n { for j in 0..i { if dp[j] && word_dict.contains(&&s[j..i]) { dp[i] = true; break; } } } dp[n] } fn main() { let s = "applepenapple"; let word_dict = ["apple", "pen"]; println!("{}", word_break(s, &word_dict)); }
Attempts:
2 left
💡 Hint
Think about whether the string can be split into words from the dictionary without leftover characters.
✗ 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 Check with No Possible Segmentation
What will this Rust code print when checking if "catsandog" can be segmented using the dictionary ["cats", "dog", "sand", "and", "cat"]?
DSA Rust
fn word_break(s: &str, word_dict: &[&str]) -> bool { let n = s.len(); let mut dp = vec![false; n + 1]; dp[0] = true; for i in 1..=n { for j in 0..i { if dp[j] && word_dict.contains(&&s[j..i]) { dp[i] = true; break; } } } dp[n] } fn main() { let s = "catsandog"; let word_dict = ["cats", "dog", "sand", "and", "cat"]; println!("{}", word_break(s, &word_dict)); }
Attempts:
2 left
💡 Hint
Check if the string can be split fully into dictionary words without leftover characters.
✗ Incorrect
The string "catsandog" cannot be segmented fully because "og" is not in the dictionary. So the output is false.
🧠 Conceptual
advancedUnderstanding Time Complexity of Word Break
What is the time complexity of the dynamic programming solution for the Word Break problem where n is the length of the string and m is the number of words in the dictionary?
Attempts:
2 left
💡 Hint
Consider the nested loops and the substring checks against the dictionary.
✗ Incorrect
The solution uses two nested loops over the string length (O(n^2)) and checks if substrings are in the dictionary (O(m * k) where k is average word length). So total is O(n^2 * m * k).
🔧 Debug
advancedIdentify the Bug in Word Break Implementation
What error will this Rust code produce when trying to run the word break function?
DSA Rust
fn word_break(s: &str, word_dict: &[&str]) -> bool { let n = s.len(); let mut dp = vec![false; n]; dp[0] = true; for i in 1..=n { for j in 0..i { if dp[j] && word_dict.contains(&&s[j..i]) { dp[i] = true; break; } } } dp[n] } fn main() { let s = "leetcode"; let word_dict = ["leet", "code"]; println!("{}", word_break(s, &word_dict)); }
Attempts:
2 left
💡 Hint
Check the size of the dp vector and the indices accessed.
✗ Incorrect
The dp vector is created with size n but accessed at dp[n], which is out of bounds causing a panic.
🚀 Application
expertNumber 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 by splitting the string at different points.
✗ Incorrect
The string can be segmented as "cats and dog" and "cat sand dog", so there are 2 ways.
