diff --git a/solution/3300-3399/3333.Find the Original Typed String II/README.md b/solution/3300-3399/3333.Find the Original Typed String II/README.md index 8244b18474e98..0c6c9779e823b 100644 --- a/solution/3300-3399/3333.Find the Original Typed String II/README.md +++ b/solution/3300-3399/3333.Find the Original Typed String II/README.md @@ -79,7 +79,24 @@ tags: -### 方法一 +### 方法一:动态规划 + 前缀和 + +长度至少为 $k$,可以拆分成两个子问题: + +- 长度不限制,那么每一组连续相同字符的长度都可以选择 $1$ 到该组长度的任意一个字符,假设方案数为 $a$。 +- 长度小于 $k$,假设方案数为 $b$。 + +那么最终的方案数为 $a - b$。 + +我们可以将字符串 $\textit{word}$ 中连续相同的字符分组,由于每组至少选择一个字符,因此,如果一组剩余可选字符大于 $0$,我们将其加入到一个数组 $\textit{nums}$ 中。初始选完每一组之后,我们更新剩余的可选字符数 $k$。 + +如果 $k < 1$,说明选择每一组的一个字符后,已经满足长度至少为 $k$ 的要求,此时答案为 $a$。 + +否则,我们需要计算 $b$ 的值。我们使用一个二维数组 $\textit{f}$,其中 $\textit{f}[i][j]$ 表示前 $i$ 组字符中,选择 $j$ 个字符的方案数。初始时 $\textit{f}[0][0] = 1$,表示没有字符时,选择 $0$ 个字符的方案数为 $1$。那么 $b = \sum_{j=0}^{k-1} \text{f}[m][j]$,其中 $m$ 为 $\textit{nums}$ 的长度。答案为 $a - b$。 + +考虑 $\textit{f}[i][j]$ 的转移方程。对于第 $i$ 组字符,假设其剩余长度为 $x$,对于每个 $j$,我们可以枚举选择该组的字符数 $l$,那么 $l \in [0, \min(x, j)]$。此时,$\textit{f}[i][j]$ 可以由 $\textit{f}[i-1][j-l]$ 转移而来。我们可以使用前缀和来优化这个转移过程。 + +时间复杂度 $O(n + k^2)$,空间复杂度 $O(k^2)$,其中 $n$ 为字符串 $\textit{word}$ 的长度。 diff --git a/solution/3300-3399/3333.Find the Original Typed String II/README_EN.md b/solution/3300-3399/3333.Find the Original Typed String II/README_EN.md index 3ddd4a5eb4420..d9238e9615da6 100644 --- a/solution/3300-3399/3333.Find the Original Typed String II/README_EN.md +++ b/solution/3300-3399/3333.Find the Original Typed String II/README_EN.md @@ -76,7 +76,24 @@ tags: -### Solution 1 +### Solution 1: Dynamic Programming + Prefix Sum + +For the constraint that the length is at least $k$, we can split it into two subproblems: + +- Without length restriction, for each group of consecutive identical characters, we can choose any number from $1$ to the length of the group. Let the number of ways be $a$. +- For length less than $k$, let the number of ways be $b$. + +Thus, the final answer is $a - b$. + +We can group consecutive identical characters in the string $\textit{word}$. Since at least one character must be chosen from each group, if a group has more than $0$ remaining selectable characters, we add it to an array $\textit{nums}$. After initially selecting one character from each group, we update the remaining required character count $k$. + +If $k < 1$, it means that after selecting one character from each group, the requirement of length at least $k$ is already satisfied, so the answer is $a$. + +Otherwise, we need to calculate the value of $b$. We use a 2D array $\textit{f}$, where $\textit{f}[i][j]$ represents the number of ways to select $j$ characters from the first $i$ groups. Initially, $\textit{f}[0][0] = 1$, meaning there is $1$ way to select $0$ characters from $0$ groups. Then $b = \sum_{j=0}^{k-1} \text{f}[m][j]$, where $m$ is the length of $\textit{nums}$. The answer is $a - b$. + +Consider the transition equation for $\textit{f}[i][j]$. For the $i$-th group of characters, suppose its remaining length is $x$. For each $j$, we can enumerate the number of characters $l$ chosen from this group, where $l \in [0, \min(x, j)]$. Then, $\textit{f}[i][j]$ can be transferred from $\textit{f}[i-1][j-l]$. We can use prefix sums to optimize this transition. + +The time complexity is $O(n + k^2)$, and the space complexity is $O(k^2)$, where $n$ is the length of the string $\textit{word}$.