0
0
DSA Cprogramming~20 mins

Word Break Problem in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Word Break Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
Afalse
Btrue
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Think about whether the string "leetcode" can be split into words from the dictionary {"leet", "code"}.
Predict Output
intermediate
2: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;
}
ARuntime error
Bfalse
CCompilation error
Dtrue
Attempts:
2 left
💡 Hint
Check if the string can be split into words from the dictionary.
Predict Output
advanced
2: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;
}
ACompilation error
Btrue
Cfalse
DRuntime error
Attempts:
2 left
💡 Hint
Try to segment the string using the dictionary words.
🧠 Conceptual
advanced
1:30remaining
Understanding the DP Array in Word Break
In the Word Break problem, what does the boolean array dp represent during the algorithm execution?
Adp[i] is true if the substring s[0..i-1] can be segmented into dictionary words
Bdp[i] is true if the substring s[i..end] can be segmented into dictionary words
Cdp[i] stores the length of the longest dictionary word ending at index i
Ddp[i] counts the number of dictionary words found up to index i
Attempts:
2 left
💡 Hint
Think about what the dp array tracks as the algorithm progresses from start to end.
🚀 Application
expert
2: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?
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
Try to find all possible segmentations of the string using the dictionary words.