LeetCode 139: Word Break

LeetCode 139: Word Break

    • [1. 📌 Problem Links](#1. 📌 Problem Links)
    • [2. 🧠 Solution Overview](#2. 🧠 Solution Overview)
    • [3. 🟢 Solution 1: Dynamic Programming (Bottom-Up)](#3. 🟢 Solution 1: Dynamic Programming (Bottom-Up))
      • [3.1. Algorithm Idea](#3.1. Algorithm Idea)
      • [3.2. Key Points](#3.2. Key Points)
      • [3.3. Java Implementation](#3.3. Java Implementation)
      • [3.4. Complexity Analysis](#3.4. Complexity Analysis)
    • [4. 🟡 Solution 2: Optimized DP with Length Pruning](#4. 🟡 Solution 2: Optimized DP with Length Pruning)
      • [4.1. Algorithm Idea](#4.1. Algorithm Idea)
      • [4.2. Key Points](#4.2. Key Points)
      • [4.3. Java Implementation](#4.3. Java Implementation)
      • [4.4. Complexity Analysis](#4.4. Complexity Analysis)
    • [5. 🔵 Solution 3: BFS with Visited Tracking](#5. 🔵 Solution 3: BFS with Visited Tracking)
      • [5.1. Algorithm Idea](#5.1. Algorithm Idea)
      • [5.2. Key Points](#5.2. Key Points)
      • [5.3. Java Implementation](#5.3. Java Implementation)
      • [5.4. Complexity Analysis](#5.4. Complexity Analysis)
    • [6. 📊 Solution Comparison](#6. 📊 Solution Comparison)
    • [7. 💡 Summary](#7. 💡 Summary)

2. 🧠 Solution Overview

This problem requires determining if a string s can be segmented into a space-separated sequence of one or more dictionary words from wordDict. The same word may be reused multiple times. Below are the main approaches:

Method Key Idea Time Complexity Space Complexity
Dynamic Programming DP array storing segmentability for each position O(n²) O(n)
BFS with Pruning Treat as graph traversal with visited optimization O(n²) O(n)
Optimized DP with Length Pruning DP with max word length optimization O(n×L) O(n)

3. 🟢 Solution 1: Dynamic Programming (Bottom-Up)

3.1. Algorithm Idea

We use a DP array where dp[i] represents whether the substring s[0...i-1] (the first i characters) can be segmented into dictionary words. The key insight is that if we can segment the substring ending at position j, and the substring from j to i is in the dictionary, then we can also segment the substring ending at position i.

3.2. Key Points

  • State Definition : dp[i] = whether first i characters can be segmented
  • State Transition : dp[i] = true if there exists j where dp[j] == true and s[j...i-1] is in wordDict
  • Initialization :
    • dp[0] = true (empty string can always be segmented)
    • All other dp[i] initialized to false
  • Processing Order: Process positions from 1 to n sequentially

3.3. Java Implementation

java 复制代码
class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        if (s == null || s.length() == 0) return false;
        
        Set<String> dict = new HashSet<>(wordDict);
        int n = s.length();
        boolean[] dp = new boolean[n + 1];
        dp[0] = true; // Base case: empty string
        
        for (int i = 1; i <= n; i++) {
            for (int j = 0; j < i; j++) {
                // If prefix [0,j) is segmentable and substring [j,i) is in dictionary
                if (dp[j] && dict.contains(s.substring(j, i))) {
                    dp[i] = true;
                    break; // No need to check other j's for this i
                }
            }
        }
        
        return dp[n];
    }
}

3.4. Complexity Analysis

  • Time Complexity : O(n²) - Two nested loops over string length
  • Space Complexity : O(n) - For DP array and dictionary set

4. 🟡 Solution 2: Optimized DP with Length Pruning

4.1. Algorithm Idea

This approach optimizes the standard DP solution by using the maximum word length from the dictionary to limit the inner loop range. This avoids unnecessary checks when the potential word length exceeds the maximum available dictionary word length.

4.2. Key Points

  • Length Pruning : Calculate maxLen - the longest word in dictionary
  • Optimized Inner Loop : Only check substrings with length ≤ maxLen
  • Reverse Iteration: Check from the end of potential words for better performance

4.3. Java Implementation

java 复制代码
class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        if (s == null || s.length() == 0) return false;
        
        Set<String> dict = new HashSet<>(wordDict);
        int n = s.length();
        
        // Calculate maximum word length for pruning
        int maxLen = 0;
        for (String word : dict) {
            maxLen = Math.max(maxLen, word.length());
        }
        
        boolean[] dp = new boolean[n + 1];
        dp[0] = true;
        
        for (int i = 1; i <= n; i++) {
            // Only check back up to maxLen characters
            int start = Math.max(0, i - maxLen);
            for (int j = i - 1; j >= start; j--) {
                if (dp[j] && dict.contains(s.substring(j, i))) {
                    dp[i] = true;
                    break;
                }
            }
        }
        
        return dp[n];
    }
}

4.4. Complexity Analysis

  • Time Complexity : O(n×L) - Where L is maxLen, typically much smaller than n
  • Space Complexity : O(n) - Same as standard DP

5. 🔵 Solution 3: BFS with Visited Tracking

5.1. Algorithm Idea

We can model this as a graph search problem where each position represents a node, and we traverse from each position to all reachable positions using dictionary words. BFS naturally finds the shortest path to the end, and we use a visited array to avoid reprocessing the same states.

5.2. Key Points

  • State Representation: Current position in the string
  • Graph Traversal: From each position, try all possible dictionary words
  • Visited Tracking: Avoid revisiting the same positions
  • Termination: Return true when we reach the end of the string

5.3. Java Implementation

java 复制代码
class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        if (s == null || s.length() == 0) return false;
        
        Set<String> dict = new HashSet<>(wordDict);
        int n = s.length();
        
        // Calculate maximum word length for pruning
        int maxLen = 0;
        for (String word : dict) {
            maxLen = Math.max(maxLen, word.length());
        }
        
        Queue<Integer> queue = new LinkedList<>();
        boolean[] visited = new boolean[n + 1];
        queue.offer(0);
        visited[0] = true;
        
        while (!queue.isEmpty()) {
            int start = queue.poll();
            
            // Try all possible end positions
            for (int end = start + 1; end <= n && end - start <= maxLen; end++) {
                if (!visited[end] && dict.contains(s.substring(start, end))) {
                    if (end == n) {
                        return true; // Reached the end
                    }
                    queue.offer(end);
                    visited[end] = true;
                }
            }
        }
        
        return false;
    }
}

5.4. Complexity Analysis

  • Time Complexity : O(n²) - Each position processed once, with up to n checks
  • Space Complexity : O(n) - For queue and visited array

6. 📊 Solution Comparison

Solution Time Space Pros Cons
Standard DP O(n²) O(n) Most intuitive, guaranteed optimal Slower for long strings
Optimized DP O(n×L) O(n) Much faster with length pruning Slightly more complex
BFS Approach O(n²) O(n) Natural graph interpretation May explore unnecessary states

7. 💡 Summary

For the Word Break problem:

  • Learning & Understanding : Start with Standard DP to grasp the fundamental state transition concept
  • Interviews & Practical Use : Optimized DP with Length Pruning offers the best performance for most scenarios
  • Alternative Perspective : BFS Approach provides a different intuition about the problem as a reachability graph

The key insight is recognizing the optimal substructure - the segmentability of a string depends on the segmentability of its prefixes and the presence of the remaining suffix in the dictionary.

Just as words give meaning to random letters, the Word Break problem teaches us that complex challenges can be solved by breaking them into smaller, recognizable patterns and systematically combining their solutions.

相关推荐
海边的Kurisu31 分钟前
苍穹外卖日记 | Day1 苍穹外卖概述、开发环境搭建、接口文档
java
C雨后彩虹4 小时前
任务最优调度
java·数据结构·算法·华为·面试
heartbeat..4 小时前
Spring AOP 全面详解(通俗易懂 + 核心知识点 + 完整案例)
java·数据库·spring·aop
Jing_jing_X4 小时前
AI分析不同阶层思维 二:Spring 的事务在什么情况下会失效?
java·spring·架构·提升·薪资
少林码僧6 小时前
2.31 机器学习神器项目实战:如何在真实项目中应用XGBoost等算法
人工智能·python·算法·机器学习·ai·数据挖掘
钱彬 (Qian Bin)6 小时前
项目实践15—全球证件智能识别系统(切换为Qwen3-VL-8B-Instruct图文多模态大模型)
人工智能·算法·机器学习·多模态·全球证件识别
元Y亨H6 小时前
Nacos - 服务发现
java·微服务
微露清风6 小时前
系统性学习C++-第十八讲-封装红黑树实现myset与mymap
java·c++·学习
dasi02276 小时前
Java趣闻
java
Niuguangshuo7 小时前
EM算法详解:解密“鸡生蛋“的机器学习困局
算法·机器学习·概率论