> For the complete documentation index, see [llms.txt](https://protegejj.gitbook.io/oj-practices/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://protegejj.gitbook.io/oj-practices/chapter1/depth-first-search/memoization/139-word-break.md).

# 139     Word Break

## 139. [Word Break](https://leetcode.com/problems/word-break/description/)

## 1. Question

Given a **non-empty** strings and a dictionary wordDict containing a list of **non-empty** words, determine if scan be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.

For example, given\
s=`"leetcode"`,\
dict=`["leet", "code"]`.

Return true because`"leetcode"`can be segmented as`"leet code"`.

## 2. Implementation

**(1) DFS + Memoization**

```java
class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        Map<Integer, Boolean> map = new HashMap<>();
        Set<String> dict = new HashSet<>(wordDict);

        return isBreakable(0, s, dict, map);
    }

    public boolean isBreakable(int start, String s, Set<String> dict, Map<Integer, Boolean> map) {
        if (start == s.length()) {
            return true;
        }

        if (map.containsKey(start)) {
            return map.get(start);
        }

        boolean breakable = false;

        for (int end = start + 1; end <= s.length(); end++) {
            if (dict.contains(s.substring(start, end)) && isBreakable(end, s, dict, map)) {
                breakable = true;
            }
        }

        map.put(start, breakable);
        return breakable;
    }
}
```

**(2) DP**

```java
class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        Set<String> dict = new HashSet<>(wordDict);
        int n = s.length();
        boolean[] dp = new boolean[n + 1];
        dp[0] = true;

        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < i; j++) {
                if (dp[j] && dict.contains(s.substring(j, i))) {
                    dp[i] = true;
                    break;
                }
            }
        }
        return dp[n];
    }
}
```

## 3. Time & Space Complexity

**DFS + Memoinzation:** 时间复杂度O(n^2), 空间复杂度O(n)

**DP**: 时间复杂度O(n^2), 空间复杂度O(n)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://protegejj.gitbook.io/oj-practices/chapter1/depth-first-search/memoization/139-word-break.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
