Skip to content

[pull] main from doocs:main #489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 213 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
213 commits
Select commit Hold shift + click to select a range
8c8d9c6
chore: update workflow
acbin Apr 25, 2025
c86e351
chore: update deploy.yml
acbin Apr 26, 2025
a6cf378
chore: update deploy workflow
acbin Apr 26, 2025
4f47587
chore: update workflows
acbin Apr 26, 2025
3aad112
chore: update .gitignore
acbin Apr 26, 2025
7fc7b5c
feat: update solutions to lc problem: No.2444 (#4373)
yanglbme Apr 26, 2025
089e43d
chore: update deploy workflow
yanglbme Apr 26, 2025
7db519a
chore: update workflows
yanglbme Apr 26, 2025
186aa5e
feat: add rust solution to lc problem: No.3392 (#4375)
yanglbme Apr 26, 2025
303a9cc
docs: add deepwiki badge (#4376)
thinkasany Apr 27, 2025
6823f28
feat: add rust solution to lc problem: No.2300 (#4377)
yanglbme Apr 27, 2025
7ccaed7
chore: auto update starcharts
idoocs Apr 29, 2025
43b825f
feat: add solutions to lc problem: No.1295 (#4378)
yanglbme Apr 29, 2025
7fb7d02
feat: add new lc problems (#4379)
yanglbme Apr 30, 2025
7d76370
feat: add solutions to lc problem: No.3528 (#4380)
yanglbme Apr 30, 2025
39eaf17
feat: add solutions to lc problem: No.3527 (#4381)
yanglbme May 1, 2025
562547b
feat: add solutions to lc problem: No.2071 (#4382)
yanglbme May 1, 2025
bed93c3
feat: add solutions to lc problem: No.0838 (#4383)
yanglbme May 2, 2025
1bf0fe3
feat: add solutions to lc problem: No.1007 (#4384)
yanglbme May 2, 2025
9877046
feat: add solutions to lc problem: No.1128 (#4386)
yanglbme May 3, 2025
c75bdf1
feat: add solutions to lc problem: No.3531 (#4387)
yanglbme May 4, 2025
2868b7d
feat: add solutions to lc problem: No.3532 (#4388)
yanglbme May 4, 2025
7cfd1f3
feat: add solutions to lc problem: No.0790 (#4389)
yanglbme May 4, 2025
c34a0d2
feat: add weekly contest 448 (#4390)
yanglbme May 5, 2025
b124017
fix: update solutions to lc problems: No.3341,3342 (#4391)
yanglbme May 6, 2025
15dd23d
chore: auto update starcharts
idoocs May 11, 2025
3f5067f
feat: add new lc problems (#4399)
yanglbme May 12, 2025
6fdcc26
feat: add solutions to lc problem: No.3541 (#4400)
yanglbme May 12, 2025
c85ac1d
feat: add solutions to lc problem: No.4545 (#4401)
yanglbme May 12, 2025
356cd7b
feat: add solutions to lc problem: No.3546 (#4402)
yanglbme May 12, 2025
170fd23
feat: add solutions to lc problem: No.3335 (#4404)
yanglbme May 13, 2025
ab51502
feat: add solutions to lc problem: No.3337 (#4405)
yanglbme May 13, 2025
b6b8817
feat: add solutions to lc problem: No.3337 (#4406)
yanglbme May 14, 2025
52f89c8
feat: update solutions to lc problems: No.2900,2901 (#4408)
yanglbme May 14, 2025
99a394b
feat: add solutions to lc problem: No.1477 (#4409)
yanglbme May 17, 2025
29ee80c
feat: update lc problems (#4410)
yanglbme May 18, 2025
a3979f7
feat: add solutions to lc problems: No.3550,3551 (#4411)
yanglbme May 18, 2025
8ec2624
feat: add solutions to lc problem: No.3552 (#4412)
yanglbme May 18, 2025
070dbb6
feat: add solutions to lc problem: No.3356 (#4413)
yanglbme May 19, 2025
49fea5c
feat: add rust solution to lc problem: No.0581 (#4414)
yanglbme May 20, 2025
81ba6c0
chore: add new lc problems (#4416)
yanglbme May 20, 2025
741bea8
feat: add solutions to lc problem: No.3555 (#4417)
yanglbme May 20, 2025
ffca226
feat: add solutions to lc problem: No.3554 (#4418)
yanglbme May 20, 2025
9dbf88f
feat: add solutions to lc problem: No.2009 (#4419)
yanglbme May 20, 2025
0482811
feat: add solutions to lc problems: No.2357,2358 (#4420)
yanglbme May 21, 2025
51a192d
feat: add solutions to lc problem: No.3362 (#4421)
yanglbme May 22, 2025
bd0eb2c
feat: update solutions to lc problem: No.3362
yanglbme May 22, 2025
9eb9ca9
feat: add c solution to lc problem: No.0001 (#4422)
pranjal030404 May 22, 2025
4217576
feat: add solutions to lc problem: No.2359 (#4423)
yanglbme May 23, 2025
6085f2f
feat: add solutions to lc problem: No.2942 (#4424)
yanglbme May 23, 2025
853735d
feat: add c solution to lc problem: No.0002 (#4426)
pranjal030404 May 23, 2025
7381ede
feat: add solutions to lc problem: No.2131 (#4428)
yanglbme May 25, 2025
6bbb968
feat: add new lc problems (#4429)
yanglbme May 25, 2025
b890898
feat: add solutions to lc problem: No.3556 (#4430)
yanglbme May 25, 2025
e188f51
feat: add weekly contest 451 (#4431)
yanglbme May 25, 2025
ec22c87
feat: add solutions to lc problem: No.1857 (#4432)
yanglbme May 25, 2025
3e3d06c
feat: add solutions to lc problem: No.0245 (#4433)
yanglbme May 25, 2025
b2a6267
feat: add c solutions to lc problems: No.0003,0004 (#4427)
pranjal030404 May 26, 2025
b5a542b
fix: resolve memory leak issue in No.0002 (#4434)
gaganxsaini May 26, 2025
1ebe39f
feat: add solutions to lc problem: No.2966 (#4435)
yanglbme May 26, 2025
40f3d8b
feat: add solutions to lc problem: No.3403 (#4436)
yanglbme May 26, 2025
afa39bd
feat: update lc problems (#4438)
yanglbme May 26, 2025
185c94f
feat: add solutions to lc problem: No.3565 (#4439)
yanglbme May 27, 2025
ce35926
feat: add solutions to lc problem: No.0147 (#4437)
taoyq1988 May 27, 2025
aca2202
chore: auto compress images (#4440)
github-actions[bot] May 27, 2025
8e777ff
feat: add solutions to lc problem: No.0269 (#4441)
taoyq1988 May 27, 2025
352832e
feat: add solutions to lc problem: No.3560 (#4443)
yanglbme May 27, 2025
30d02ab
feat: add solutions to lc problem: No.3561 (#4444)
yanglbme May 27, 2025
7c2ed94
feat: add rust solutions to lc problems: No.3372,3373 (#4445)
yanglbme May 27, 2025
8aca291
feat: add cpp solution to lc problem No.0008 (#4442)
gaganxsaini May 27, 2025
6ae2c07
chore: auto update starcharts
idoocs May 28, 2025
dd076f7
chore: add missing image (#4447)
yanglbme May 28, 2025
18565c4
feat: add solutions to lc problem: No.1061 (#4448)
yanglbme May 28, 2025
c7a3051
feat: update lc problems (#4449)
yanglbme May 28, 2025
3ddd4aa
feat: add solutions to lc problem: No.2359 (#4450)
yanglbme May 29, 2025
c6b4e43
feat: add solutions to lc problem: No.0909 (#4451)
yanglbme May 31, 2025
8c4bba2
feat: add solutions to lc problem: No.2929 (#4452)
yanglbme Jun 1, 2025
15f7077
feat: add new lc problems (#4453)
yanglbme Jun 2, 2025
90f55e9
feat: add solutions to lc problems: No.3566,3567 (#4454)
yanglbme Jun 2, 2025
befa1c5
feat: add solutions to lc problem: No.3568 (#4455)
yanglbme Jun 2, 2025
9716c9c
feat: add rust solution to lc problem: No.135 (#4456)
yanglbme Jun 2, 2025
13ca332
feat: add solutions to lc problem: No.1298 (#4457)
yanglbme Jun 2, 2025
490508b
feat: update lc problems (#4459)
yanglbme Jun 3, 2025
297e28c
feat: add solutions to lc problem: No.2616 (#4460)
yanglbme Jun 3, 2025
c520c5a
feat: add solutions to lc problem: No.3406 (#4461)
yanglbme Jun 4, 2025
6558818
feat: add solutions to lc problem: No.1061 (#4462)
yanglbme Jun 4, 2025
2e938eb
feat: add solutions to lc problem: No.3442 (#4463)
yanglbme Jun 4, 2025
58a5c21
feat: add solutions to lc problem: No.3405 (#4464)
yanglbme Jun 5, 2025
a774cc7
feat: add solutions to lc problem: No.2434 (#4465)
yanglbme Jun 5, 2025
ba46ac8
feat: add solutions to lc problem: No.3170 (#4466)
yanglbme Jun 7, 2025
078c02d
feat: add new lc problems (#4467)
yanglbme Jun 8, 2025
f5db36b
feat: add solutions to lc problem: No.3573 (#4468)
yanglbme Jun 8, 2025
26b007e
feat: add solutions to lc problem: No.0440 (#4469)
yanglbme Jun 8, 2025
82583d6
feat: add solutions to lc problem: No.3576 (#4470)
yanglbme Jun 9, 2025
57362c1
feat: update lc problems (#4471)
yanglbme Jun 9, 2025
ec0dc28
chore: update problems (#4472)
yanglbme Jun 9, 2025
76a5f03
feat: add c solutions to lc problems: No.0005,0006 (#4458)
pranjal030404 Jun 9, 2025
ccd5497
feat: update solution to lc problem: No.3580 (#4473)
yanglbme Jun 9, 2025
8f9449a
feat: add solutions to lc problem: No.3578 (#4475)
yanglbme Jun 10, 2025
24da320
chore(deps): bump requests from 2.32.0 to 2.32.4 (#4474)
dependabot[bot] Jun 10, 2025
363aafd
feat: add solutions to lc problem: No.3445 (#4476)
yanglbme Jun 11, 2025
a0a5cb7
feat: update lc problems (#4477)
yanglbme Jun 11, 2025
e26e506
feat: update solutions to lc problem: No.0006 (#4478)
yanglbme Jun 11, 2025
4aaa775
docs: add support us section
yanglbme Jun 11, 2025
f352441
chore: auto compress images (#4479)
github-actions[bot] Jun 11, 2025
2f33e2a
docs: update readme guideline (#4480)
yanglbme Jun 11, 2025
dc357fd
feat: add solutions to lc problem: No.3579 (#4481)
yanglbme Jun 11, 2025
22c6a2b
chore: auto update starcharts
idoocs Jun 11, 2025
e51cd08
feat: add rust solution to lc problem: No.3579 (#4482)
yanglbme Jun 11, 2025
128977c
feat: add solutions to lc problem: No.3423 (#4483)
yanglbme Jun 11, 2025
102ce22
feat: add rust solutions to lc problems: No.3573,3577,3578 (#4484)
yanglbme Jun 12, 2025
a1a4b07
feat: add solutions to lc problem: No.3574 (#4486)
yanglbme Jun 12, 2025
2d63f55
feat: add solutions to lc problem: No.0281 (#4485)
taoyq1988 Jun 12, 2025
2976139
feat: add solutions to lc problem: No.2040 (#4487)
yanglbme Jun 12, 2025
80d9354
feat: add solutions to lc problem: No.3424 (#4488)
yanglbme Jun 12, 2025
6cf042d
feat: add solutions to lc problem: No.2616 (#4489)
yanglbme Jun 12, 2025
370d053
feat: add solutions to lc problem: No.2614 (#4490)
yanglbme Jun 12, 2025
0c33e0b
feat: add solutions to lc problem: No.2566 (#4492)
yanglbme Jun 14, 2025
40ca1e8
fix: correct case-sensitivity issue in lc problem No.1517 (#4495)
yanglbme Jun 15, 2025
cc5ff74
feat: add solutions to lc problem: No.1432 (#4496)
yanglbme Jun 15, 2025
add0319
feat: add solutions to lc problems: No.3581,3582 (#4497)
yanglbme Jun 15, 2025
e68e1fd
feat: add new lc problems (#4498)
yanglbme Jun 16, 2025
53d1406
feat: add solutions to lc problem: No.3586 (#4499)
yanglbme Jun 16, 2025
43a2a84
feat: add solutions to lc problem: No.3584 (#4500)
yanglbme Jun 16, 2025
589c8db
feat: add solutions to lc problem: No.3581 (#4501)
yanglbme Jun 16, 2025
2c123b1
feat: add rust solutions to lc problems: No.2894,3581 (#4502)
yanglbme Jun 16, 2025
0bfadf8
feat: add solutions to lc problem: No.3405 (#4503)
yanglbme Jun 16, 2025
9de9346
feat: update lc problems (#4504)
yanglbme Jun 16, 2025
ac79b4f
feat: add rust solution to lc problem: No.2967 (#4505)
yanglbme Jun 17, 2025
590343f
feat: add c solutions to lc problems: No.0008,0009 (#4506)
pranjal030404 Jun 18, 2025
0fcbf87
feat: add solutions to lc problem: No.2294 (#4507)
yanglbme Jun 18, 2025
fe3eeb4
feat: add c solutions to lc problems: No.0010,0011 (#4508)
pranjal030404 Jun 19, 2025
9dcd740
feat: add rust solution to lc problem: No.3443 (#4509)
yanglbme Jun 19, 2025
6d918ed
feat: add php solution to lc problem: No.0010 (#4510)
yanglbme Jun 19, 2025
28cace0
feat: add c solutions to lc problems: No.0012,0013 (#4511)
pranjal030404 Jun 20, 2025
76cb00e
feat: add solutions to lc problem: No.3085 (#4512)
yanglbme Jun 20, 2025
e59e051
feat: add biweekly contest 159 (#4513)
yanglbme Jun 22, 2025
5f828dc
feat: add solutions to lc problem: No.3587 (#4514)
yanglbme Jun 22, 2025
0960310
feat: add weekly contest 455 (#4515)
yanglbme Jun 22, 2025
a7a652d
feat: add solutions to lc problem: No.1751 (#4516)
yanglbme Jun 22, 2025
186c8d3
feat: add solutions to lc problem: No.2081 (#4517)
yanglbme Jun 22, 2025
42061ce
chore: auto update starcharts
idoocs Jun 23, 2025
2bc4cdf
feat: add python solution to lc problem: No.3590 (#4518)
Ishanssr Jun 23, 2025
18a93fa
feat: add rust solutions to lc problem: No.2200 (#4520)
yanglbme Jun 23, 2025
05ffb40
feat: add c solutions to lc problems: No.0014,0015 (#4519)
pranjal030404 Jun 24, 2025
ac084bb
feat: add rust solution to lc problem: No.2040 (#4521)
yanglbme Jun 24, 2025
90f466e
feat: add new lc problem (#4522)
yanglbme Jun 24, 2025
5ded9f3
feat: update lc problems (#4523)
yanglbme Jun 25, 2025
6ef2369
feat: add rust solution to lc problem: No.2311 (#4524)
yanglbme Jun 25, 2025
9a9eb75
feat: add rust solution to lc problem: No.3330 (#4525)
yanglbme Jun 25, 2025
8c4ea1f
feat: add solutions to lc problem: No.2014 (#4526)
yanglbme Jun 26, 2025
56b3edb
feat: add solutions to lc problem: No.2099 (#4527)
yanglbme Jun 27, 2025
5ea879f
chore: use setAll func to initialize array in Java solution (#4528)
yanglbme Jun 27, 2025
74b8a69
feat: add solutions to lc problem: No.3696 (#4529)
yanglbme Jun 28, 2025
0bf2e51
feat: add rust solution to lc problem: No.3307 (#4530)
yanglbme Jun 28, 2025
bafd167
feat: add solutions to lc problem: No.1498 (#4531)
yanglbme Jun 29, 2025
a358d8f
feat: add weekly contest 456 (#4532)
yanglbme Jun 29, 2025
fe2396a
feat: add solutions to lc problem: No.3597 (#4534)
yanglbme Jun 29, 2025
4ae8f79
feat: add solutions to lc problem: No.3598 (#4535)
yanglbme Jun 29, 2025
e1541b3
feat: add solutions to lc problem: No.3599 (#4536)
yanglbme Jun 29, 2025
5b01d2f
feat: add rust solution to lc problem: No.0594 (#4537)
yanglbme Jun 29, 2025
13f8867
feat: add new lc problems (#4538)
yanglbme Jul 1, 2025
10461e4
feat: update lc problems (#4539)
yanglbme Jul 1, 2025
e8fc106
feat: add solutions to lc problem: No.3333 (#4540)
yanglbme Jul 1, 2025
11b9cba
feat: add solutions to lc problem: No.3333 (#4541)
yanglbme Jul 1, 2025
280a4b6
feat: add rust solution to lc problem: No.3304 (#4542)
yanglbme Jul 2, 2025
c692eef
feat: add solutions to lc problem: No.3601 (#4543)
yanglbme Jul 2, 2025
db8cc79
feat: add solutions to lc problem: No.3307 (#4545)
yanglbme Jul 3, 2025
e63bbb4
feat: add c solutions to lc problems: No.0016,0017 (#4544)
pranjal030404 Jul 3, 2025
d4b3ea3
feat: add solutions to lc problem: No.3439 (#4546)
yanglbme Jul 4, 2025
b3ce308
feat: add solutions to lc problem: No.1394 (#4547)
yanglbme Jul 4, 2025
9ab3cee
feat: add rust solution to lc problem: No.1865 (#4550)
yanglbme Jul 6, 2025
b7d00fe
feat: add biweekly contest 160 (#4551)
yanglbme Jul 6, 2025
2b5348d
chore: auto compress images (#4552)
github-actions[bot] Jul 6, 2025
82a0dd5
feat: add weekly contest 457 (#4553)
yanglbme Jul 6, 2025
b2220d0
feat: add solutions to lc problem: No.1353 (#4554)
yanglbme Jul 6, 2025
0f5ed3f
feat: add solutions to lc problem: No.3602 (#4555)
yanglbme Jul 6, 2025
15c59ee
chore: auto update starcharts
idoocs Jul 7, 2025
55d4ea1
feat: add rust solution to lc problem: No.1751 (#4556)
yanglbme Jul 7, 2025
d7fddea
feat: add solutions to lc problem: No.3606 (#4558)
yanglbme Jul 8, 2025
7ad3c3b
feat: add solutions to lc problem: No.3440 (#4559)
yanglbme Jul 10, 2025
3e49812
feat: add rust solution to lc problem: No.3169 (#4560)
yanglbme Jul 10, 2025
a70fbdb
feat: add solutions to lc problem: No.3611 (#4561)
yanglbme Jul 11, 2025
68ab63d
feat: add solutions to lc problem: No.1900 (#4562)
yanglbme Jul 12, 2025
b61fed3
chore: update lc problems (#4563)
yanglbme Jul 12, 2025
3e0f23e
feat: add rust solution to lc problem: No.2410 (#4564)
yanglbme Jul 12, 2025
3c5a7b2
feat: add weekly contest 458 (#4565)
yanglbme Jul 13, 2025
e9531d1
feat: add solutions to lc problems: No.3612,3613 (#4566)
yanglbme Jul 13, 2025
5c30065
feat: add solutions to lc problem: No.3616 (#4567)
yanglbme Jul 13, 2025
a666697
feat: add solutions to lc problem: No.1290 (#4568)
yanglbme Jul 13, 2025
6f7b0b3
feat: add solutions to lc problem: No.3136 (#4569)
yanglbme Jul 14, 2025
97d7b82
feat: add solutions to lc problem: No.3617 (#4570)
yanglbme Jul 14, 2025
47cb088
feat: add solutions to lc problems: No.3201,3202 (#4571)
yanglbme Jul 15, 2025
4db3791
feat: add solutions to lc problem: No.3203 (#4572)
yanglbme Jul 16, 2025
cf8049e
feat: add solutions to lc problem: No.2322 (#4573)
yanglbme Jul 17, 2025
5a94bc4
feat: add solutions to lc problem: No.2163 (#4574)
yanglbme Jul 17, 2025
de776ba
fix: update solutions to lc problems: No.1046,1354 (#4575)
yanglbme Jul 17, 2025
39c895b
feat: add solutions to lc problem: No.1233 (#4576)
yanglbme Jul 18, 2025
1b53d4d
fix: update solutions to lc problems: No.0630,0973,1962,2856 (#4577)
yanglbme Jul 18, 2025
c10a2f1
feat: add new lc problems (#4579)
yanglbme Jul 20, 2025
d369cf9
feat: add solution for deleting duplicate folders in a file system (#…
Speccy-Rom Jul 20, 2025
ed31865
feat: add solutions to lc problem: No.1948 (#4582)
yanglbme Jul 20, 2025
8c7fd24
feat: add solutions to lc problem: No.3618 (#4583)
yanglbme Jul 20, 2025
0fe1388
feat: add solutions to lc problem: No.3619 (#4586)
yanglbme Jul 20, 2025
2604b21
feat: update solutions to lc problem: No.1957 (#4587)
yanglbme Jul 20, 2025
60245f5
feat: add solutions to lc problem: No.3622 (#4588)
yanglbme Jul 20, 2025
f4c6fcf
feat: add rust solutions to lc problem: No.1695 (#4590)
yanglbme Jul 21, 2025
7cff981
feat: add solutions to lc problem: No.1960 (#4585)
samarthswami1016 Jul 22, 2025
246dbd3
feat: add solutions to lc problem: No.1717 (#4591)
yanglbme Jul 22, 2025
5464fea
chore: auto update starcharts
idoocs Jul 23, 2025
2dcb224
feat: add solution to lc problem: No.2322 (#4592)
yanglbme Jul 23, 2025
65f3542
feat: update lc problems (#4593)
yanglbme Jul 23, 2025
6569141
feat: add solutions to lc problems: No.3487,3488 (#4594)
yanglbme Jul 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add rust solution to lc problem: No.3579 (doocs#4482)
No.3579.Minimum Steps to Convert String with Operations
  • Loading branch information
yanglbme authored Jun 11, 2025
commit e51cd086536e48137d57b1265b63910a1527f6a0
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,17 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:贪心 + 动态规划

我们定义 $f[i]$ 表示将 $\textit{word1}$ 的前 $i$ 个字符转换为 $\textit{word2}$ 的前 $i$ 个字符所需的最小操作数。那么答案为 $f[n]$,其中 $n$ 是 $\textit{word1}$ 和 $\textit{word2}$ 的长度。

我们可以通过遍历所有可能的分割点来计算 $f[i]$。对于每个分割点 $j$,我们需要计算将 $\textit{word1}[j:i]$ 转换为 $\textit{word2}[j:i]$ 所需的最小操作数。

我们可以使用一个辅助函数 $\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})))$。

接下来我们需要实现 $\text{calc}(l, r, \text{rev})$ 函数。我们用一个二维数组 $cnt$ 来记录 $\textit{word1}$ 和 $\textit{word2}$ 中字符的配对情况。对于每个字符对 $(a, b)$,如果 $a \neq b$,我们需要检查 $cnt[b][a]$ 是否大于 $0$。如果是,我们可以将其配对,减少一次操作;否则,我们需要增加一次操作,并将 $cnt[a][b]$ 加 $1$。

时间复杂度 $O(n^3 + |\Sigma|^2)$,空间复杂度 $O(n + |\Sigma|^2)$,其中 $n$ 是字符串的长度,而 $|\Sigma|$ 是字符集大小(本题中为 $26$)。

<!-- tabs:start -->

Expand Down Expand Up @@ -357,6 +367,53 @@ function minOperations(word1: string, word2: string): number {
}
```

#### Rust

```rust
impl Solution {
pub fn min_operations(word1: String, word2: String) -> i32 {
let n = word1.len();
let word1 = word1.as_bytes();
let word2 = word2.as_bytes();
let mut f = vec![i32::MAX; n + 1];
f[0] = 0;

for i in 1..=n {
for j in 0..i {
let a = Self::calc(word1, word2, j, i - 1, false);
let b = 1 + Self::calc(word1, word2, j, i - 1, true);
let t = a.min(b);
f[i] = f[i].min(f[j] + t);
}
}

f[n]
}

fn calc(word1: &[u8], word2: &[u8], l: usize, r: usize, rev: bool) -> i32 {
let mut cnt = [[0i32; 26]; 26];
let mut res = 0;

for i in l..=r {
let j = if rev { r - (i - l) } else { i };
let a = (word1[j] - b'a') as usize;
let b = (word2[i] - b'a') as usize;

if a != b {
if cnt[b][a] > 0 {
cnt[b][a] -= 1;
} else {
cnt[a][b] += 1;
res += 1;
}
}
}

res
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,17 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Greedy + Dynamic Programming

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}$.

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]$.

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})))$.

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$.

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).

<!-- tabs:start -->

Expand Down Expand Up @@ -352,6 +362,53 @@ function minOperations(word1: string, word2: string): number {
}
```

#### Rust

```rust
impl Solution {
pub fn min_operations(word1: String, word2: String) -> i32 {
let n = word1.len();
let word1 = word1.as_bytes();
let word2 = word2.as_bytes();
let mut f = vec![i32::MAX; n + 1];
f[0] = 0;

for i in 1..=n {
for j in 0..i {
let a = Self::calc(word1, word2, j, i - 1, false);
let b = 1 + Self::calc(word1, word2, j, i - 1, true);
let t = a.min(b);
f[i] = f[i].min(f[j] + t);
}
}

f[n]
}

fn calc(word1: &[u8], word2: &[u8], l: usize, r: usize, rev: bool) -> i32 {
let mut cnt = [[0i32; 26]; 26];
let mut res = 0;

for i in l..=r {
let j = if rev { r - (i - l) } else { i };
let a = (word1[j] - b'a') as usize;
let b = (word2[i] - b'a') as usize;

if a != b {
if cnt[b][a] > 0 {
cnt[b][a] -= 1;
} else {
cnt[a][b] += 1;
res += 1;
}
}
}

res
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
impl Solution {
pub fn min_operations(word1: String, word2: String) -> i32 {
let n = word1.len();
let word1 = word1.as_bytes();
let word2 = word2.as_bytes();
let mut f = vec![i32::MAX; n + 1];
f[0] = 0;

for i in 1..=n {
for j in 0..i {
let a = Self::calc(word1, word2, j, i - 1, false);
let b = 1 + Self::calc(word1, word2, j, i - 1, true);
let t = a.min(b);
f[i] = f[i].min(f[j] + t);
}
}

f[n]
}

fn calc(word1: &[u8], word2: &[u8], l: usize, r: usize, rev: bool) -> i32 {
let mut cnt = [[0i32; 26]; 26];
let mut res = 0;

for i in l..=r {
let j = if rev { r - (i - l) } else { i };
let a = (word1[j] - b'a') as usize;
let b = (word2[i] - b'a') as usize;

if a != b {
if cnt[b][a] > 0 {
cnt[b][a] -= 1;
} else {
cnt[a][b] += 1;
res += 1;
}
}
}

res
}
}