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 C# code that checks if the string can be segmented into dictionary words?
DSA C#
using System; using System.Collections.Generic; class Program { static bool WordBreak(string s, HashSet<string> dict) { bool[] dp = new bool[s.Length + 1]; dp[0] = true; for (int i = 1; i <= s.Length; i++) { for (int j = 0; j < i; j++) { if (dp[j] && dict.Contains(s.Substring(j, i - j))) { dp[i] = true; break; } } } return dp[s.Length]; } static void Main() { var dict = new HashSet<string> {"leet", "code"}; string s = "leetcode"; Console.WriteLine(WordBreak(s, dict)); } }
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
intermediateOutput for Unbreakable String
What will be printed by this C# program that tests word break for a string that cannot be segmented?
DSA C#
using System; using System.Collections.Generic; class Program { static bool WordBreak(string s, HashSet<string> dict) { bool[] dp = new bool[s.Length + 1]; dp[0] = true; for (int i = 1; i <= s.Length; i++) { for (int j = 0; j < i; j++) { if (dp[j] && dict.Contains(s.Substring(j, i - j))) { dp[i] = true; break; } } } return dp[s.Length]; } static void Main() { var dict = new HashSet<string> {"apple", "pen"}; string s = "applepenapplex"; Console.WriteLine(WordBreak(s, dict)); } }
Attempts:
2 left
💡 Hint
Check if the string "applepenapplex" can be fully segmented using "apple" and "pen".
✗ Incorrect
The string ends with 'x' which is not in the dictionary, so it cannot be segmented fully. The output is False.
🔧 Debug
advancedIdentify the Logical Error in Word Break Implementation
The following code attempts to solve the Word Break problem but produces incorrect results. What is the logical error?
DSA C#
using System; using System.Collections.Generic; class Program { static bool WordBreak(string s, HashSet<string> dict) { bool[] dp = new bool[s.Length + 1]; dp[0] = true; for (int i = 1; i <= s.Length; i++) { for (int j = 0; j < i; j++) { if (dict.Contains(s.Substring(j, i - j))) { dp[i] = true; break; } } } return dp[s.Length]; } static void Main() { var dict = new HashSet<string> {"cats", "dog", "sand", "and", "cat"}; string s = "catsanddog"; Console.WriteLine(WordBreak(s, dict)); } }
Attempts:
2 left
💡 Hint
Check the condition inside the inner loop carefully.
✗ Incorrect
The code misses checking dp[j], which means it does not verify if the string before the current substring can be segmented. This causes incorrect true values.
🧠 Conceptual
advancedTime Complexity of Word Break Algorithm
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 substring extraction cost and dictionary lookup cost inside nested loops.
✗ Incorrect
The outer loop runs n times, inner loop up to n times, substring extraction is O(k) where k is substring length (average word length), dictionary lookup is O(1). So total is O(n^2 * k).
🚀 Application
expertCount All Possible Word Breaks
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 valid segmentations of the string using the dictionary words.
✗ Incorrect
The string can be segmented as "cats and dog" and "cat sand dog" and "cat sanddog" is invalid because "sanddog" is not in dict. So total 2 ways. But careful: "catsanddog" can be segmented in 2 ways only.
