feat: add solutions to lc problem: No.3579 (#4481)

No.3579.Minimum Steps to Convert String with Operations
This commit is contained in:
Libin YANG 2025-06-11 17:36:42 +08:00 committed by GitHub
parent 2f33e2afb2
commit dc357fdb73
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 554 additions and 0 deletions

View File

@ -151,25 +151,210 @@ tags:
#### Python3
```python
class Solution:
def minOperations(self, word1: str, word2: str) -> int:
def calc(l: int, r: int, rev: bool) -> int:
cnt = Counter()
res = 0
for i in range(l, r + 1):
j = r - (i - l) if rev else i
a, b = word1[j], word2[i]
if a != b:
if cnt[(b, a)] > 0:
cnt[(b, a)] -= 1
else:
cnt[(a, b)] += 1
res += 1
return res
n = len(word1)
f = [inf] * (n + 1)
f[0] = 0
for i in range(1, n + 1):
for j in range(i):
t = min(calc(j, i - 1, False), 1 + calc(j, i - 1, True))
f[i] = min(f[i], f[j] + t)
return f[n]
```
#### Java
```java
class Solution {
public int minOperations(String word1, String word2) {
int n = word1.length();
int[] f = new int[n + 1];
Arrays.fill(f, Integer.MAX_VALUE);
f[0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
int a = calc(word1, word2, j, i - 1, false);
int b = 1 + calc(word1, word2, j, i - 1, true);
int t = Math.min(a, b);
f[i] = Math.min(f[i], f[j] + t);
}
}
return f[n];
}
private int calc(String word1, String word2, int l, int r, boolean rev) {
int[][] cnt = new int[26][26];
int res = 0;
for (int i = l; i <= r; i++) {
int j = rev ? r - (i - l) : i;
int a = word1.charAt(j) - 'a';
int b = word2.charAt(i) - 'a';
if (a != b) {
if (cnt[b][a] > 0) {
cnt[b][a]--;
} else {
cnt[a][b]++;
res++;
}
}
}
return res;
}
}
```
#### C++
```cpp
class Solution {
public:
int minOperations(string word1, string word2) {
int n = word1.length();
vector<int> f(n + 1, INT_MAX);
f[0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
int a = calc(word1, word2, j, i - 1, false);
int b = 1 + calc(word1, word2, j, i - 1, true);
int t = min(a, b);
f[i] = min(f[i], f[j] + t);
}
}
return f[n];
}
private:
int calc(const string& word1, const string& word2, int l, int r, bool rev) {
int cnt[26][26] = {0};
int res = 0;
for (int i = l; i <= r; ++i) {
int j = rev ? r - (i - l) : i;
int a = word1[j] - 'a';
int b = word2[i] - 'a';
if (a != b) {
if (cnt[b][a] > 0) {
cnt[b][a]--;
} else {
cnt[a][b]++;
res++;
}
}
}
return res;
}
};
```
#### Go
```go
func minOperations(word1 string, word2 string) int {
n := len(word1)
f := make([]int, n+1)
for i := range f {
f[i] = math.MaxInt32
}
f[0] = 0
calc := func(l, r int, rev bool) int {
var cnt [26][26]int
res := 0
for i := l; i <= r; i++ {
j := i
if rev {
j = r - (i - l)
}
a := word1[j] - 'a'
b := word2[i] - 'a'
if a != b {
if cnt[b][a] > 0 {
cnt[b][a]--
} else {
cnt[a][b]++
res++
}
}
}
return res
}
for i := 1; i <= n; i++ {
for j := 0; j < i; j++ {
a := calc(j, i-1, false)
b := 1 + calc(j, i-1, true)
t := min(a, b)
f[i] = min(f[i], f[j]+t)
}
}
return f[n]
}
```
#### TypeScript
```ts
function minOperations(word1: string, word2: string): number {
const n = word1.length;
const f = Array(n + 1).fill(Number.MAX_SAFE_INTEGER);
f[0] = 0;
function calc(l: number, r: number, rev: boolean): number {
const cnt: number[][] = Array.from({ length: 26 }, () => Array(26).fill(0));
let res = 0;
for (let i = l; i <= r; i++) {
const j = rev ? r - (i - l) : i;
const a = word1.charCodeAt(j) - 97;
const b = word2.charCodeAt(i) - 97;
if (a !== b) {
if (cnt[b][a] > 0) {
cnt[b][a]--;
} else {
cnt[a][b]++;
res++;
}
}
}
return res;
}
for (let i = 1; i <= n; i++) {
for (let j = 0; j < i; j++) {
const a = calc(j, i - 1, false);
const b = 1 + calc(j, i - 1, true);
const t = Math.min(a, b);
f[i] = Math.min(f[i], f[j] + t);
}
}
return f[n];
}
```
<!-- tabs:end -->

View File

@ -146,25 +146,210 @@ tags:
#### Python3
```python
class Solution:
def minOperations(self, word1: str, word2: str) -> int:
def calc(l: int, r: int, rev: bool) -> int:
cnt = Counter()
res = 0
for i in range(l, r + 1):
j = r - (i - l) if rev else i
a, b = word1[j], word2[i]
if a != b:
if cnt[(b, a)] > 0:
cnt[(b, a)] -= 1
else:
cnt[(a, b)] += 1
res += 1
return res
n = len(word1)
f = [inf] * (n + 1)
f[0] = 0
for i in range(1, n + 1):
for j in range(i):
t = min(calc(j, i - 1, False), 1 + calc(j, i - 1, True))
f[i] = min(f[i], f[j] + t)
return f[n]
```
#### Java
```java
class Solution {
public int minOperations(String word1, String word2) {
int n = word1.length();
int[] f = new int[n + 1];
Arrays.fill(f, Integer.MAX_VALUE);
f[0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
int a = calc(word1, word2, j, i - 1, false);
int b = 1 + calc(word1, word2, j, i - 1, true);
int t = Math.min(a, b);
f[i] = Math.min(f[i], f[j] + t);
}
}
return f[n];
}
private int calc(String word1, String word2, int l, int r, boolean rev) {
int[][] cnt = new int[26][26];
int res = 0;
for (int i = l; i <= r; i++) {
int j = rev ? r - (i - l) : i;
int a = word1.charAt(j) - 'a';
int b = word2.charAt(i) - 'a';
if (a != b) {
if (cnt[b][a] > 0) {
cnt[b][a]--;
} else {
cnt[a][b]++;
res++;
}
}
}
return res;
}
}
```
#### C++
```cpp
class Solution {
public:
int minOperations(string word1, string word2) {
int n = word1.length();
vector<int> f(n + 1, INT_MAX);
f[0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
int a = calc(word1, word2, j, i - 1, false);
int b = 1 + calc(word1, word2, j, i - 1, true);
int t = min(a, b);
f[i] = min(f[i], f[j] + t);
}
}
return f[n];
}
private:
int calc(const string& word1, const string& word2, int l, int r, bool rev) {
int cnt[26][26] = {0};
int res = 0;
for (int i = l; i <= r; ++i) {
int j = rev ? r - (i - l) : i;
int a = word1[j] - 'a';
int b = word2[i] - 'a';
if (a != b) {
if (cnt[b][a] > 0) {
cnt[b][a]--;
} else {
cnt[a][b]++;
res++;
}
}
}
return res;
}
};
```
#### Go
```go
func minOperations(word1 string, word2 string) int {
n := len(word1)
f := make([]int, n+1)
for i := range f {
f[i] = math.MaxInt32
}
f[0] = 0
calc := func(l, r int, rev bool) int {
var cnt [26][26]int
res := 0
for i := l; i <= r; i++ {
j := i
if rev {
j = r - (i - l)
}
a := word1[j] - 'a'
b := word2[i] - 'a'
if a != b {
if cnt[b][a] > 0 {
cnt[b][a]--
} else {
cnt[a][b]++
res++
}
}
}
return res
}
for i := 1; i <= n; i++ {
for j := 0; j < i; j++ {
a := calc(j, i-1, false)
b := 1 + calc(j, i-1, true)
t := min(a, b)
f[i] = min(f[i], f[j]+t)
}
}
return f[n]
}
```
#### TypeScript
```ts
function minOperations(word1: string, word2: string): number {
const n = word1.length;
const f = Array(n + 1).fill(Number.MAX_SAFE_INTEGER);
f[0] = 0;
function calc(l: number, r: number, rev: boolean): number {
const cnt: number[][] = Array.from({ length: 26 }, () => Array(26).fill(0));
let res = 0;
for (let i = l; i <= r; i++) {
const j = rev ? r - (i - l) : i;
const a = word1.charCodeAt(j) - 97;
const b = word2.charCodeAt(i) - 97;
if (a !== b) {
if (cnt[b][a] > 0) {
cnt[b][a]--;
} else {
cnt[a][b]++;
res++;
}
}
}
return res;
}
for (let i = 1; i <= n; i++) {
for (let j = 0; j < i; j++) {
const a = calc(j, i - 1, false);
const b = 1 + calc(j, i - 1, true);
const t = Math.min(a, b);
f[i] = Math.min(f[i], f[j] + t);
}
}
return f[n];
}
```
<!-- tabs:end -->

View File

@ -0,0 +1,42 @@
class Solution {
public:
int minOperations(string word1, string word2) {
int n = word1.length();
vector<int> f(n + 1, INT_MAX);
f[0] = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < i; ++j) {
int a = calc(word1, word2, j, i - 1, false);
int b = 1 + calc(word1, word2, j, i - 1, true);
int t = min(a, b);
f[i] = min(f[i], f[j] + t);
}
}
return f[n];
}
private:
int calc(const string& word1, const string& word2, int l, int r, bool rev) {
int cnt[26][26] = {0};
int res = 0;
for (int i = l; i <= r; ++i) {
int j = rev ? r - (i - l) : i;
int a = word1[j] - 'a';
int b = word2[i] - 'a';
if (a != b) {
if (cnt[b][a] > 0) {
cnt[b][a]--;
} else {
cnt[a][b]++;
res++;
}
}
}
return res;
}
};

View File

@ -0,0 +1,44 @@
func minOperations(word1 string, word2 string) int {
n := len(word1)
f := make([]int, n+1)
for i := range f {
f[i] = math.MaxInt32
}
f[0] = 0
calc := func(l, r int, rev bool) int {
var cnt [26][26]int
res := 0
for i := l; i <= r; i++ {
j := i
if rev {
j = r - (i - l)
}
a := word1[j] - 'a'
b := word2[i] - 'a'
if a != b {
if cnt[b][a] > 0 {
cnt[b][a]--
} else {
cnt[a][b]++
res++
}
}
}
return res
}
for i := 1; i <= n; i++ {
for j := 0; j < i; j++ {
a := calc(j, i-1, false)
b := 1 + calc(j, i-1, true)
t := min(a, b)
f[i] = min(f[i], f[j]+t)
}
}
return f[n]
}

View File

@ -0,0 +1,36 @@
class Solution {
public int minOperations(String word1, String word2) {
int n = word1.length();
int[] f = new int[n + 1];
Arrays.fill(f, Integer.MAX_VALUE);
f[0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < i; j++) {
int a = calc(word1, word2, j, i - 1, false);
int b = 1 + calc(word1, word2, j, i - 1, true);
int t = Math.min(a, b);
f[i] = Math.min(f[i], f[j] + t);
}
}
return f[n];
}
private int calc(String word1, String word2, int l, int r, boolean rev) {
int[][] cnt = new int[26][26];
int res = 0;
for (int i = l; i <= r; i++) {
int j = rev ? r - (i - l) : i;
int a = word1.charAt(j) - 'a';
int b = word2.charAt(i) - 'a';
if (a != b) {
if (cnt[b][a] > 0) {
cnt[b][a]--;
} else {
cnt[a][b]++;
res++;
}
}
}
return res;
}
}

View File

@ -0,0 +1,24 @@
class Solution:
def minOperations(self, word1: str, word2: str) -> int:
def calc(l: int, r: int, rev: bool) -> int:
cnt = Counter()
res = 0
for i in range(l, r + 1):
j = r - (i - l) if rev else i
a, b = word1[j], word2[i]
if a != b:
if cnt[(b, a)] > 0:
cnt[(b, a)] -= 1
else:
cnt[(a, b)] += 1
res += 1
return res
n = len(word1)
f = [inf] * (n + 1)
f[0] = 0
for i in range(1, n + 1):
for j in range(i):
t = min(calc(j, i - 1, False), 1 + calc(j, i - 1, True))
f[i] = min(f[i], f[j] + t)
return f[n]

View File

@ -0,0 +1,38 @@
function minOperations(word1: string, word2: string): number {
const n = word1.length;
const f = Array(n + 1).fill(Number.MAX_SAFE_INTEGER);
f[0] = 0;
function calc(l: number, r: number, rev: boolean): number {
const cnt: number[][] = Array.from({ length: 26 }, () => Array(26).fill(0));
let res = 0;
for (let i = l; i <= r; i++) {
const j = rev ? r - (i - l) : i;
const a = word1.charCodeAt(j) - 97;
const b = word2.charCodeAt(i) - 97;
if (a !== b) {
if (cnt[b][a] > 0) {
cnt[b][a]--;
} else {
cnt[a][b]++;
res++;
}
}
}
return res;
}
for (let i = 1; i <= n; i++) {
for (let j = 0; j < i; j++) {
const a = calc(j, i - 1, false);
const b = 1 + calc(j, i - 1, true);
const t = Math.min(a, b);
f[i] = Math.min(f[i], f[j] + t);
}
}
return f[n];
}