View on NeetCode
View on LeetCode

Problem

Given a string s and a dictionary of strings wordDict, return true if s can be segmented into a space-separated sequence of one or more dictionary words.

Note that the same word in the dictionary may be reused multiple times in the segmentation.

Example 1:

Input: s = "leetcode", wordDict = ["leet","code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".

Example 2:

Input: s = "applepenapple", wordDict = ["apple","pen"]
Output: true
Explanation: Return true because "applepenapple" can be segmented as "apple pen apple".
Note that you are allowed to reuse a dictionary word.

Example 3:

Input: s = "catsandog", wordDict = ["cats","dog","sand","and","cat"]
Output: false

Constraints:

  • 1 <= s.length <= 300
  • 1 <= wordDict.length <= 1000
  • 1 <= wordDict[i].length <= 20
  • s and wordDict[i] consist of only lowercase English letters.
  • All the strings of wordDict are unique.

Solution

Approach 1: Dynamic Programming (Bottom-Up)

The key insight is that s[0:i] can be segmented if there exists a position j where s[0:j] can be segmented and s[j:i] is in the dictionary.

Implementation

class Solution:
    def wordBreak(self, s: str, wordDict: List[str]) -> bool:
        word_set = set(wordDict)
        n = len(s)
        dp = [False] * (n + 1)
        dp[0] = True  # Empty string can be segmented

        for i in range(1, n + 1):
            for j in range(i):
                if dp[j] and s[j:i] in word_set:
                    dp[i] = True
                    break

        return dp[n]

Approach 2: Recursion with Memoization (Top-Down)

class Solution:
    def wordBreak(self, s: str, wordDict: List[str]) -> bool:
        word_set = set(wordDict)
        memo = {}

        def dp(start):
            if start == len(s):
                return True
            if start in memo:
                return memo[start]

            for end in range(start + 1, len(s) + 1):
                if s[start:end] in word_set and dp(end):
                    memo[start] = True
                    return True

            memo[start] = False
            return False

        return dp(0)

Approach 3: BFS

from collections import deque

class Solution:
    def wordBreak(self, s: str, wordDict: List[str]) -> bool:
        word_set = set(wordDict)
        queue = deque([0])
        visited = set([0])

        while queue:
            start = queue.popleft()

            if start == len(s):
                return True

            for end in range(start + 1, len(s) + 1):
                if end not in visited and s[start:end] in word_set:
                    queue.append(end)
                    visited.add(end)

        return False

Complexity Analysis

Bottom-Up DP:

  • Time Complexity: O(n² * m), where n is length of s and m is average length of words (for substring comparison).
  • Space Complexity: O(n) for the dp array, plus O(k) for word set where k is total characters in wordDict.

Top-Down DP:

  • Time Complexity: O(n²), with memoization we process each substring once.
  • Space Complexity: O(n) for memoization and recursion stack.

BFS:

  • Time Complexity: O(n²), exploring all possible segmentations.
  • Space Complexity: O(n) for queue and visited set.

Key Insights

  1. Optimal Substructure: If s[0:i] can be segmented, then s[0:i+k] can be segmented if s[i:i+k] is in dictionary.

  2. DP State: dp[i] represents whether s[0:i] can be segmented.

  3. Two Pointers: For each position i, check all previous positions j to see if s[j:i] is a valid word and s[0:j] can be segmented.

  4. Set for Fast Lookup: Convert wordDict to set for O(1) lookup instead of O(k) list search.

  5. BFS Interpretation: Treat positions as nodes, and add edge from i to j if s[i:j] is in dictionary. Find path from 0 to n.

  6. Memoization: Avoid recomputing whether substrings starting at same position can be segmented.