139. 单词拆分(Word Break)

给定字符串 s 和字符串字典 wordDict,判断 s 是否可以被拆分为一个或多个字典中的单词。

特点:

  • 字典单词 可以重复使用

  • 不要求使用所有字典单词


方法一:暴力 DFS(递归)

思路

从字符串开头开始:

  1. 枚举前缀

  2. 如果前缀在字典中

  3. 递归判断剩余部分

例如:

复制代码
applepenapple

apple | penapple
      pen | apple

如果能拆分到最后返回 true


C 代码

复制代码
#include <stdbool.h>
#include <string.h>

bool inDict(char* s, int start, int len, char** wordDict, int wordDictSize)
{
    for(int i = 0; i < wordDictSize; i++)
    {
        if(strlen(wordDict[i]) == len &&
           strncmp(s + start, wordDict[i], len) == 0)
            return true;
    }
    return false;
}

bool dfs(char* s, int start, int len, char** wordDict, int wordDictSize)
{
    if(start == len) return true;

    for(int i = start + 1; i <= len; i++)
    {
        if(inDict(s, start, i - start, wordDict, wordDictSize) &&
           dfs(s, i, len, wordDict, wordDictSize))
            return true;
    }

    return false;
}

bool wordBreak(char* s, char** wordDict, int wordDictSize)
{
    return dfs(s, 0, strlen(s), wordDict, wordDictSize);
}

复杂度

复制代码
时间复杂度:O(2^n)
空间复杂度:O(n)

因为大量重复计算,容易 超时


方法二:DFS + 记忆化搜索(剪枝)

思路

暴力 DFS 的问题:

复制代码
重复计算同一个子问题

例如:

复制代码
applepenapple
       apple

我们可以记录:

复制代码
start 位置是否可以拆分

用数组 memo[] 记录状态:

复制代码
0 = 未计算
1 = 可以拆分
-1 = 不可以拆分

C 代码

复制代码
#include <stdbool.h>
#include <string.h>

int memo[305];

bool inDict(char* s, int start, int len, char** wordDict, int wordDictSize)
{
    for(int i = 0; i < wordDictSize; i++)
    {
        if(strlen(wordDict[i]) == len &&
           strncmp(s + start, wordDict[i], len) == 0)
            return true;
    }
    return false;
}

bool dfs(char* s, int start, int len, char** wordDict, int wordDictSize)
{
    if(start == len) return true;

    if(memo[start] != 0)
        return memo[start] == 1;

    for(int i = start + 1; i <= len; i++)
    {
        if(inDict(s, start, i - start, wordDict, wordDictSize) &&
           dfs(s, i, len, wordDict, wordDictSize))
        {
            memo[start] = 1;
            return true;
        }
    }

    memo[start] = -1;
    return false;
}

bool wordBreak(char* s, char** wordDict, int wordDictSize)
{
    memset(memo, 0, sizeof(memo));
    return dfs(s, 0, strlen(s), wordDict, wordDictSize);
}

复杂度

复制代码
时间复杂度:O(n² * m)
空间复杂度:O(n)

方法三:动态规划(最经典)

这是 LeetCode 官方最推荐方法


DP 思路

定义:

复制代码
dp[i] 表示 s[0..i-1] 是否可以拆分

初始化:

复制代码
dp[0] = true

状态转移:

复制代码
dp[i] = dp[j] && s[j..i-1] 在字典中

其中:

复制代码
0 ≤ j < i

C 代码

复制代码
#include <stdbool.h>
#include <string.h>

bool inDict(char* s, int start, int len, char** wordDict, int wordDictSize)
{
    for(int i = 0; i < wordDictSize; i++)
    {
        if(strlen(wordDict[i]) == len &&
           strncmp(s + start, wordDict[i], len) == 0)
            return true;
    }
    return false;
}

bool wordBreak(char* s, char** wordDict, int wordDictSize)
{
    int n = strlen(s);

    bool dp[n + 1];
    memset(dp, false, sizeof(dp));

    dp[0] = true;

    for(int i = 1; i <= n; i++)
    {
        for(int j = 0; j < i; j++)
        {
            if(dp[j] && inDict(s, j, i - j, wordDict, wordDictSize))
            {
                dp[i] = true;
                break;
            }
        }
    }

    return dp[n];
}

复杂度

复制代码
时间复杂度:O(n² * m)
空间复杂度:O(n)

方法四:DP + 哈希优化(最优解)

优化点:

1️⃣ 使用 HashSet 加速查找

2️⃣ 限制单词最大长度

题目给出:

复制代码
wordDict[i].length ≤ 20

因此:

复制代码
j >= i - 20

减少无效判断。


优化版 C 代码

复制代码
#include <stdbool.h>
#include <string.h>

bool inDict(char* s, int start, int len, char** wordDict, int wordDictSize)
{
    for(int i = 0; i < wordDictSize; i++)
    {
        if(strlen(wordDict[i]) == len &&
           strncmp(s + start, wordDict[i], len) == 0)
            return true;
    }
    return false;
}

bool wordBreak(char* s, char** wordDict, int wordDictSize)
{
    int n = strlen(s);

    bool dp[n + 1];
    memset(dp, false, sizeof(dp));

    dp[0] = true;

    for(int i = 1; i <= n; i++)
    {
        for(int j = i - 20; j < i; j++)
        {
            if(j >= 0 && dp[j] &&
               inDict(s, j, i - j, wordDict, wordDictSize))
            {
                dp[i] = true;
                break;
            }
        }
    }

    return dp[n];
}

DP过程图解

示例:

复制代码
s = "leetcode"
dict = ["leet","code"]

索引:

复制代码
0 1 2 3 4 5 6 7 8
l e e t c o d e

DP数组:

复制代码
T F F F T F F F T

解释:

复制代码
dp[4] = true  (leet)
dp[8] = true  (code)

面试总结

最推荐回答顺序:

复制代码
1 暴力DFS
2 DFS + 记忆化
3 动态规划(重点)
4 DP优化

面试官最希望听到:

复制代码
DP 状态定义 + 状态转移
相关推荐
宵时待雨5 小时前
回溯算法专题1:递归
数据结构·c++·笔记·算法·leetcode·深度优先
爱思德学术5 小时前
【SPIE出版】黄冈师范学院主办!第四届大数据、计算智能与应用国际会议(BDCIA 2026)
大数据·算法·数据分析·云计算·etl
洛水水5 小时前
【力扣100题】40.二叉树中的最大路径和
算法·leetcode·深度优先
洛水水5 小时前
【力扣100题】37.从前序与中序遍历序列构造二叉树
c++·算法·leetcode
zyq99101_15 小时前
递归与动态规划实战代码解析
python·算法·蓝桥杯
橘白3165 小时前
rl笔记(一):策略梯度更新算法推导
人工智能·算法·机器人·强化学习
hhhhhaaa5 小时前
多节点矩阵式任务系统:统一配置中心与动态规则引擎架构设计
后端·算法·架构
吃着火锅x唱着歌5 小时前
LeetCode 739.每日温度
算法·leetcode·职场和发展
如竟没有火炬5 小时前
去除重复字母——贪心+单调栈
开发语言·数据结构·python·算法·leetcode·深度优先
薛定e的猫咪5 小时前
【ICML 2025】MODULI:基于扩散模型解锁离线多目标强化学习的偏好泛化
人工智能·学习·算法·机器学习