0
0
DSA Typescriptprogramming~10 mins

Word Break Problem in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the string can be segmented by dictionary words.

DSA Typescript
function wordBreak(s: string, wordDict: string[]): boolean {
  const dp: boolean[] = Array(s.length + 1).fill(false);
  dp[0] = true;
  for (let i = 1; i <= s.length; i++) {
    for (let j = 0; j < i; j++) {
      if (dp[j] && wordDict.includes(s.substring(j, [1]))) {
        dp[i] = true;
        break;
      }
    }
  }
  return dp[s.length];
}
Drag options to blanks, or click blank then click option'
A0
Bi
Cs.length
Dj
Attempts:
3 left
💡 Hint
Common Mistakes
Using j instead of i causes checking empty or wrong substring.
Using s.length causes out of range substring.
2fill in blank
medium

Complete the code to initialize the dp array correctly.

DSA Typescript
function wordBreak(s: string, wordDict: string[]): boolean {
  const dp: boolean[] = Array([1]).fill(false);
  dp[0] = true;
  // rest of the code
}
Drag options to blanks, or click blank then click option'
As.length + 1
BwordDict.length
Cs.length
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using s.length causes index out of range errors.
Using wordDict.length is unrelated to dp size.
3fill in blank
hard

Fix the error in the loop condition to avoid infinite loops.

DSA Typescript
for (let i = 1; [1]; i++) {
  for (let j = 0; j < i; j++) {
    // code
  }
}
Drag options to blanks, or click blank then click option'
Ai <= s.length
Bi < s.length
Ci >= s.length
Di > s.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using i < s.length causes missing last substring checks.
Using i >= s.length or i > s.length causes infinite or no loops.
4fill in blank
hard

Fill both blanks to correctly check substring and update dp array.

DSA Typescript
if (dp[[1]] && wordDict.includes(s.substring([2], i))) {
  dp[i] = true;
  break;
}
Drag options to blanks, or click blank then click option'
Aj
Bi
C0
Ds.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using i in dp or substring start causes wrong checks.
Using 0 or s.length causes incorrect substring ranges.
5fill in blank
hard

Fill all three blanks to complete the word break function correctly.

DSA Typescript
function wordBreak(s: string, wordDict: string[]): boolean {
  const dp: boolean[] = Array([1]).fill(false);
  dp[0] = true;
  for (let i = 1; i [2] s.length; i++) {
    for (let j = 0; j < i; j++) {
      if (dp[[3]] && wordDict.includes(s.substring(j, i))) {
        dp[i] = true;
        break;
      }
    }
  }
  return dp[s.length];
}
Drag options to blanks, or click blank then click option'
As.length + 1
B<=
Cj
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using dp size as s.length causes index errors.
Using i < s.length misses last character.
Using dp[i] instead of dp[j] causes logic errors.