📖 Scenario: Imagine you have a long string of letters without spaces, and you want to check if it can be split into valid words from a dictionary. This is like trying to read a message where spaces are missing and figuring out if it makes sense.
🎯 Goal: You will build a program that checks if a given string can be segmented into a sequence of dictionary words. This helps understand how to use arrays and loops to solve problems step-by-step.
📋 What You'll Learn
Create a string variable called input with the exact value "applepenapple".
Create an array of strings called dictionary with these exact words: "apple", "pen".
Create an integer variable called dictSize and set it to 2.
Create an integer array called dp with size strlen(input) + 1.
Write a loop to fill dp with zeros and set dp[0] to 1.
Use nested loops to check all substrings of input and update dp to mark valid breaks.
Print "true" if the whole string can be segmented, otherwise print "false".
💡 Why This Matters
🌍 Real World
This problem helps in text processing tasks like spell checking, auto-correct, and natural language processing where words need to be identified in a continuous text.
💼 Career
Understanding this problem shows your ability to use arrays, loops, and string functions in C, which are essential skills for software development and technical interviews.
Progress0 / 4 steps
1
Create the input string and dictionary
Create a string variable called input with the value "applepenapple". Create an array of strings called dictionary with the words "apple" and "pen". Create an integer variable called dictSize and set it to 2.
DSA C
Hint
Use char input[] = "applepenapple"; to create the input string. Use char *dictionary[] = {"apple", "pen"}; for the dictionary words. Set dictSize to 2.
2
Create the dp array and initialize it
Create an integer array called dp with size strlen(input) + 1. Write a loop to fill dp with zeros and set dp[0] to 1.
DSA C
Hint
Use int dp[100]; to create the array. Use a for loop from 0 to strlen(input) to set all dp[i] to 0. Then set dp[0] = 1;.
3
Implement the word break logic
Use nested for loops with variables i and j to check all substrings of input. For each substring, check if dp[j] is 1 and if the substring input[j..i-1] matches any word in dictionary. If yes, set dp[i] to 1.
DSA C
Hint
Use two loops: outer loop i from 1 to length, inner loop j from 0 to i-1. Check if dp[j] is 1 and substring matches a dictionary word using strncmp. If yes, set dp[i] = 1.
4
Print the result
Write a printf statement to print "true" if dp[length] is 1, otherwise print "false".
DSA C
Hint
Use if (dp[length]) to check if the string can be segmented. Print "true" if yes, otherwise print "false".
Practice
(1/5)
1. What does the Word Break Problem ask you to determine about a given string s and a dictionary of words wordDict?
easy
A. If s contains any substring from wordDict
B. If s can be segmented into a space-separated sequence of one or more dictionary words
C. If s is exactly equal to any word in wordDict
D. If s is a palindrome
Solution
Step 1: Understand the problem statement
The Word Break Problem asks if the entire string s can be split into parts where each part is a word in the dictionary.
Step 2: Compare options with the problem
If s can be segmented into a space-separated sequence of one or more dictionary words correctly describes this segmentation into dictionary words. Other options describe different checks.
Final Answer:
If s can be segmented into a space-separated sequence of one or more dictionary words -> Option B
Quick Check:
Word Break Problem = segmentation check [OK]
Hint: Remember: Word Break means full segmentation into dictionary words [OK]
Common Mistakes:
Thinking it checks for substring presence only
Confusing exact match with segmentation
Assuming palindrome check
2. Which of the following C code snippets correctly declares a function prototype for solving the Word Break Problem?
easy
A. int wordBreak(char s[], char *wordDict[], int wordDictSize);
B. void wordBreak(char s[], char wordDict[], int size);
C. int wordBreak(char *s, char *wordDict, int wordDictSize);
D. int wordBreak(char **s, char **wordDict, int wordDictSize);
Solution
Step 1: Understand parameter types for the Word Break function
The string s is a character array, so char s[] or char *s works. The dictionary is an array of strings, so char *wordDict[] is correct. The size is an integer.
Step 2: Check each option
int wordBreak(char s[], char *wordDict[], int wordDictSize); matches the correct signature: int wordBreak(char s[], char *wordDict[], int wordDictSize); int wordBreak(char *s, char *wordDict, int wordDictSize); uses char *wordDict which is incorrect for array of strings. void wordBreak(char s[], char wordDict[], int size); uses char wordDict[] which is incorrect for array of strings. int wordBreak(char **s, char **wordDict, int wordDictSize); uses char **s which is incorrect for a single string.
Final Answer:
int wordBreak(char s[], char *wordDict[], int wordDictSize); -> Option A
Quick Check:
Function prototype with string and array of strings = int wordBreak(char s[], char *wordDict[], int wordDictSize); [OK]
Hint: Dictionary is array of strings: use char *wordDict[] [OK]
Common Mistakes:
Using char wordDict[] instead of array of strings
Using char **s for input string
Wrong return type or missing parameters
3. What will be the output of the following C code snippet for the Word Break Problem?
char *dict[] = {"leet", "code"};
int result = wordBreak("leetcode", dict, 2);
printf("%d", result);
Assuming wordBreak returns 1 if segmentation is possible, else 0.
medium
A. 1
B. 0
C. Compilation error
D. Runtime error
Solution
Step 1: Analyze the input string and dictionary
The string is "leetcode" and dictionary contains "leet" and "code".
Step 2: Check if string can be segmented
"leetcode" can be split as "leet" + "code", both in dictionary, so segmentation is possible.
Final Answer:
1 -> Option A
Quick Check:
"leetcode" segmented as "leet" + "code" = 1 [OK]
Hint: Check if string splits fully into dictionary words [OK]
Common Mistakes:
Assuming partial match is enough
Confusing return values
Ignoring dictionary contents
4. Identify the error in the following C code snippet for the Word Break Problem:
int wordBreak(char *s, char **wordDict, int wordDictSize) {
int len = strlen(s);
int dp[len+1];
dp[0] = 1;
for (int i = 1; i <= len; i++) {
dp[i] = 0;
for (int j = 0; j < i; j++) {
if (dp[j] && strncmp(s+j, wordDict[i], i-j) == 0) {
dp[i] = 1;
break;
}
}
}
return dp[len];
}
medium
A. Missing return statement
B. dp array not initialized properly
C. Using wordDict[i] instead of wordDict[j] in strncmp
D. Loop indices are out of range
Solution
Step 1: Understand the nested loop and string comparison
The inner loop checks substrings from index j to i. It should compare with wordDict[j], not wordDict[i].
Step 2: Identify the incorrect array index usage
Using wordDict[i] is wrong because i goes up to length of s, which may exceed wordDictSize and is unrelated to dictionary indexing.
Final Answer:
Using wordDict[i] instead of wordDict[j] in strncmp -> Option C
Quick Check:
Index in dictionary should match substring start index [OK]
Hint: Check indices used for dictionary access in loops [OK]
Common Mistakes:
Mixing loop variables for dictionary access
Not initializing dp array correctly
Ignoring return statement
5. Given the string s = "applepenapple" and dictionary wordDict = ["apple", "pen"], which approach correctly solves the Word Break Problem using dynamic programming in C?
hard
A. Use recursion without memoization to check all substring splits
B. Use a stack to store all dictionary words and pop when matched
C. Sort the dictionary and check if s starts with any word
D. Use a boolean dp array where dp[i] is true if s[0..i-1] can be segmented, then check all substrings ending at i
Solution
Step 1: Understand dynamic programming approach
We create a boolean dp array where dp[i] is true if substring s[0..i-1] can be segmented into dictionary words.
Step 2: Check all substrings ending at each position
For each position i, check all j < i where dp[j] is true and substring s[j..i-1] is in dictionary. If yes, set dp[i] true.
Step 3: Return dp[length of s]
If dp at the end is true, the string can be segmented.
Final Answer:
Use a boolean dp array where dp[i] is true if s[0..i-1] can be segmented, then check all substrings ending at i -> Option D
Quick Check:
DP boolean array with substring checks = Use a boolean dp array where dp[i] is true if s[0..i-1] can be segmented, then check all substrings ending at i [OK]
Hint: Use dp array to track segmentation up to each index [OK]
Common Mistakes:
Using recursion without memo causing timeout
Sorting dictionary does not help segmentation
Stack approach is incorrect for this problem
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.