Skip to content
/ leetcode Public
  • Sponsor doocs/leetcode

  • Notifications You must be signed in to change notification settings
  • Fork 9.1k
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3cc14f7

Browse files
committedJun 11, 2025·
feat: add solutions to lc problem: No.3579
No.3579.Minimum Steps to Convert String with Operations
1 parent 22c6a2b commit 3cc14f7

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed
 

‎solution/3500-3599/3579.Minimum Steps to Convert String with Operations/README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,17 @@ tags:
144144

145145
<!-- solution:start -->
146146

147-
### 方法一
147+
### 方法一:贪心 + 动态规划
148+
149+
我们定义 $f[i]$ 表示将 $\textit{word1}$ 的前 $i$ 个字符转换为 $\textit{word2}$ 的前 $i$ 个字符所需的最小操作数。那么答案为 $f[n]$,其中 $n$ 是 $\textit{word1}$ 和 $\textit{word2}$ 的长度。
150+
151+
我们可以通过遍历所有可能的分割点来计算 $f[i]$。对于每个分割点 $j$,我们需要计算将 $\textit{word1}[j:i]$ 转换为 $\textit{word2}[j:i]$ 所需的最小操作数。
152+
153+
我们可以使用一个辅助函数 $\text{calc}(l, r, \text{rev})$ 来计算从 $\textit{word1}[l:r]$ 转换为 $\textit{word2}[l:r]$ 所需的最小操作数,其中 $\text{rev}$ 表示是否需要反转子串。由于反转前后进行其它操作的结果是一样的,所以我们可以考虑不反转,以及首先进行一次反转后再进行其它操作。因此有 $f[i] = \min_{j < i} (f[j] + \min(\text{calc}(j, i-1, \text{false}), 1 + \text{calc}(j, i-1, \text{true})))$。
154+
155+
接下来我们需要实现 $\text{calc}(l, r, \text{rev})$ 函数。我们用一个二维数组 $cnt$ 来记录 $\textit{word1}$ 和 $\textit{word2}$ 中字符的配对情况。对于每个字符对 $(a, b)$,如果 $a \neq b$,我们需要检查 $cnt[b][a]$ 是否大于 $0$。如果是,我们可以将其配对,减少一次操作;否则,我们需要增加一次操作,并将 $cnt[a][b]$ 加 $1$。
156+
157+
时间复杂度 $O(n^3 + |\Sigma|^2)$,空间复杂度 $O(n + |\Sigma|^2)$,其中 $n$ 是字符串的长度,而 $|\Sigma|$ 是字符集大小(本题中为 $26$)。
148158

149159
<!-- tabs:start -->
150160

‎solution/3500-3599/3579.Minimum Steps to Convert String with Operations/README_EN.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,17 @@ tags:
139139

140140
<!-- solution:start -->
141141

142-
### Solution 1
142+
### Solution 1: Greedy + Dynamic Programming
143+
144+
We define $f[i]$ as the minimum number of operations required to convert the first $i$ characters of $\textit{word1}$ to the first $i$ characters of $\textit{word2}$. The answer is $f[n]$, where $n$ is the length of both $\textit{word1}$ and $\textit{word2}$.
145+
146+
We can compute $f[i]$ by enumerating all possible split points. For each split point $j$, we need to calculate the minimum number of operations required to convert $\textit{word1}[j:i]$ to $\textit{word2}[j:i]$.
147+
148+
We can use a helper function $\text{calc}(l, r, \text{rev})$ to compute the minimum number of operations needed to convert $\textit{word1}[l:r]$ to $\textit{word2}[l:r]$, where $\text{rev}$ indicates whether to reverse the substring. Since the result of performing other operations before or after a reversal is the same, we only need to consider not reversing, and reversing once before other operations. Therefore, $f[i] = \min_{j < i} (f[j] + \min(\text{calc}(j, i-1, \text{false}), 1 + \text{calc}(j, i-1, \text{true})))$.
149+
150+
Next, we need to implement the $\text{calc}(l, r, \text{rev})$ function. We use a 2D array $cnt$ to record the pairing status of characters between $\textit{word1}$ and $\textit{word2}$. For each character pair $(a, b)$, if $a \neq b$, we check whether $cnt[b][a] > 0$. If so, we can pair them and reduce one operation; otherwise, we need to add one operation and increment $cnt[a][b]$ by $1$.
151+
152+
The time complexity is $O(n^3 + |\Sigma|^2)$ and the space complexity is $O(n + |\Sigma|^2)$, where $n$ is the length of the string and $|\Sigma|$ is the size of the character set (which is $26$ in this problem).
143153

144154
<!-- tabs:start -->
145155

0 commit comments

Comments
 (0)
Please sign in to comment.