LeetCode 题解工作台

字符串中的额外字符

给你一个下标从 0 开始的字符串 s 和一个单词字典 dictionary 。你需要将 s 分割成若干个 互不重叠 的子字符串,每个子字符串都在 dictionary 中出现过。 s 中可能会有一些 额外的字符 不在任何子字符串中。 请你采取最优策略分割 s ,使剩下的字符 最少 。 示例 1: 输…

category

5

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·哈希·扫描

bolt

答案摘要

我们可以用一个哈希表 记录字段中的所有单词,方便我们快速判断一个字符串是否在字典中。 接下来,我们定义 表示字符串 的前 个字符的最小额外字符数,初始时 $f[0] = 0$。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个下标从 0 开始的字符串 s 和一个单词字典 dictionary 。你需要将 s 分割成若干个 互不重叠 的子字符串,每个子字符串都在 dictionary 中出现过。s 中可能会有一些 额外的字符 不在任何子字符串中。

请你采取最优策略分割 s ,使剩下的字符 最少 。

 

示例 1:

输入:s = "leetscode", dictionary = ["leet","code","leetcode"]
输出:1
解释:将 s 分成两个子字符串:下标从 0 到 3 的 "leet" 和下标从 5 到 8 的 "code" 。只有 1 个字符没有使用(下标为 4),所以我们返回 1 。

示例 2:

输入:s = "sayhelloworld", dictionary = ["hello","world"]
输出:3
解释:将 s 分成两个子字符串:下标从 3 到 7 的 "hello" 和下标从 8 到 12 的 "world" 。下标为 0 ,1 和 2 的字符没有使用,所以我们返回 3 。

 

提示:

  • 1 <= s.length <= 50
  • 1 <= dictionary.length <= 50
  • 1 <= dictionary[i].length <= 50
  • dictionary[i] 和 s 只包含小写英文字母。
  • dictionary 中的单词互不相同。
lightbulb

解题思路

方法一:哈希表 + 动态规划

我们可以用一个哈希表 ssss 记录字段中的所有单词,方便我们快速判断一个字符串是否在字典中。

接下来,我们定义 f[i]f[i] 表示字符串 ss 的前 ii 个字符的最小额外字符数,初始时 f[0]=0f[0] = 0

i1i \ge 1 时,第 ii 个字符 s[i1]s[i - 1] 可以作为一个额外字符,此时 f[i]=f[i1]+1f[i] = f[i - 1] + 1,如果在 j[0,i1]j \in [0, i - 1] 中存在一个下标 jj,使得 s[j..i)s[j..i) 在哈希表 ssss 中,那么我们可以将 s[j..i)s[j..i) 作为一个单词,此时 f[i]=f[j]f[i] = f[j]

综上,我们可以得到状态转移方程:

f[i]=min{f[i1]+1,minj[0,i1]f[j]}f[i] = \min \{ f[i - 1] + 1, \min_{j \in [0, i - 1]} f[j] \}

其中 i1i \ge 1,而 j[0,i1]j \in [0, i - 1]s[j..i)s[j..i) 在哈希表 ssss 中。

最终答案为 f[n]f[n]

时间复杂度 O(n3+L)O(n^3 + L),空间复杂度 O(n+L)O(n + L)。其中 nn 是字符串 ss 的长度,而 LL 是字典中所有单词的长度之和。

1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
    def minExtraChar(self, s: str, dictionary: List[str]) -> int:
        ss = set(dictionary)
        n = len(s)
        f = [0] * (n + 1)
        for i in range(1, n + 1):
            f[i] = f[i - 1] + 1
            for j in range(i):
                if s[j:i] in ss and f[j] < f[i]:
                    f[i] = f[j]
        return f[n]
speed

复杂度分析

指标
时间O(N^2 + M \cdot K)
空间O(N + M \cdot K)
psychology

面试官常问的追问

外企场景
  • question_mark

    Can the candidate explain the reasoning behind the use of dynamic programming here?

  • question_mark

    Does the candidate demonstrate a good understanding of the pattern of breaking down a string into dictionary words?

  • question_mark

    Is the candidate able to optimize the solution by minimizing extra characters efficiently?

warning

常见陷阱

外企场景
  • error

    Failing to optimize the solution, leading to unnecessary extra characters left over.

  • error

    Overcomplicating the problem by not recognizing the value of hashing dictionary words for efficient lookups.

  • error

    Not managing dynamic programming states correctly, leading to incorrect results.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Handling a dictionary with very large numbers of words.

  • arrow_right_alt

    Optimizing the solution for longer strings with many possible splits.

  • arrow_right_alt

    Implementing the solution without relying on dynamic programming for a more brute force approach.

help

常见问题

外企场景

字符串中的额外字符题解:数组·哈希·扫描 | LeetCode #2707 中等