Skip to content

Commit 11b9cba

Browse files
authored
feat: add solutions to lc problem: No.3333 (doocs#4541)
No.3333.Find the Original Typed String II
1 parent e8fc106 commit 11b9cba

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

solution/3300-3399/3333.Find the Original Typed String II/README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,24 @@ tags:
7979

8080
<!-- solution:start -->
8181

82-
### 方法一
82+
### 方法一:动态规划 + 前缀和
83+
84+
长度至少为 $k$,可以拆分成两个子问题:
85+
86+
- 长度不限制,那么每一组连续相同字符的长度都可以选择 $1$ 到该组长度的任意一个字符,假设方案数为 $a$。
87+
- 长度小于 $k$,假设方案数为 $b$。
88+
89+
那么最终的方案数为 $a - b$。
90+
91+
我们可以将字符串 $\textit{word}$ 中连续相同的字符分组,由于每组至少选择一个字符,因此,如果一组剩余可选字符大于 $0$,我们将其加入到一个数组 $\textit{nums}$ 中。初始选完每一组之后,我们更新剩余的可选字符数 $k$。
92+
93+
如果 $k < 1$,说明选择每一组的一个字符后,已经满足长度至少为 $k$ 的要求,此时答案为 $a$。
94+
95+
否则,我们需要计算 $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$。
96+
97+
考虑 $\textit{f}[i][j]$ 的转移方程。对于第 $i$ 组字符,假设其剩余长度为 $x$,对于每个 $j$,我们可以枚举选择该组的字符数 $l$,那么 $l \in [0, \min(x, j)]$。此时,$\textit{f}[i][j]$ 可以由 $\textit{f}[i-1][j-l]$ 转移而来。我们可以使用前缀和来优化这个转移过程。
98+
99+
时间复杂度 $O(n + k^2)$,空间复杂度 $O(k^2)$,其中 $n$ 为字符串 $\textit{word}$ 的长度。
83100

84101
<!-- tabs:start -->
85102

solution/3300-3399/3333.Find the Original Typed String II/README_EN.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,24 @@ tags:
7676

7777
<!-- solution:start -->
7878

79-
### Solution 1
79+
### Solution 1: Dynamic Programming + Prefix Sum
80+
81+
For the constraint that the length is at least $k$, we can split it into two subproblems:
82+
83+
- 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$.
84+
- For length less than $k$, let the number of ways be $b$.
85+
86+
Thus, the final answer is $a - b$.
87+
88+
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$.
89+
90+
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$.
91+
92+
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$.
93+
94+
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.
95+
96+
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}$.
8097

8198
<!-- tabs:start -->
8299

0 commit comments

Comments
 (0)