Solving Word Break using DP.

Problem

Can string be segmented.

Pattern

This problem demonstrates the DP pattern.

Approach

dp[i] = any dp[j] && dict.contains(s[j:i]).

Solution

  // Solution for Word Break
// Pattern: DP
// O(n²) time
  

Complexity

O(n²) time

Best Practices

  • Identify the pattern before coding — pattern recognition saves time
  • Handle edge cases: empty input, single element, duplicates
  • Use descriptive variable names even in timed interviews
  • Test with the provided examples plus one custom case