Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

s =
wordDict =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

DP over prefix: can we split up to index i?

Approach

dp[i] = True if s[:i] can be segmented. Transition: dp[i] = any(dp[i-l] and s[i-l:i] in dict). Use maxLen to cap inner loop.

Why it works

A valid segmentation must end with some dictionary word.

Code
Loading...
Complexity
Time: O(n * maxWordLen)
Space: O(n)
Solution
Updated: 2026-02-23

Idea

DP over prefix: can we split up to index i?

Approach

dp[i] = True if s[:i] can be segmented. Transition: dp[i] = any(dp[i-l] and s[i-l:i] in dict). Use maxLen to cap inner loop.

Why it works

A valid segmentation must end with some dictionary word.

Code
Loading...
Complexity
Time: O(n * maxWordLen)
Space: O(n)