-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2896-ApplyOperationsToMakeTwoStringsEqual.go
More file actions
100 lines (88 loc) · 3.57 KB
/
2896-ApplyOperationsToMakeTwoStringsEqual.go
File metadata and controls
100 lines (88 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
// 2896. Apply Operations to Make Two Strings Equal
// You are given two 0-indexed binary strings s1 and s2, both of length n, and a positive integer x.
// You can perform any of the following operations on the string s1 any number of times:
// 1. Choose two indices i and j, and flip both s1[i] and s1[j]. The cost of this operation is x.
// 2. Choose an index i such that i < n - 1 and flip both s1[i] and s1[i + 1]. The cost of this operation is 1.
// Return the minimum cost needed to make the strings s1 and s2 equal, or return -1 if it is impossible.
// Note that flipping a character means changing it from 0 to 1 or vice-versa.
// Example 1:
// Input: s1 = "1100011000", s2 = "0101001010", x = 2
// Output: 4
// Explanation: We can do the following operations:
// - Choose i = 3 and apply the second operation. The resulting string is s1 = "1101111000".
// - Choose i = 4 and apply the second operation. The resulting string is s1 = "1101001000".
// - Choose i = 0 and j = 8 and apply the first operation. The resulting string is s1 = "0101001010" = s2.
// The total cost is 1 + 1 + 2 = 4. It can be shown that it is the minimum cost possible.
// Example 2:
// Input: s1 = "10110", s2 = "00011", x = 4
// Output: -1
// Explanation: It is not possible to make the two strings equal.
// Constraints:
// n == s1.length == s2.length
// 1 <= n, x <= 500
// s1 and s2 consist only of the characters '0' and '1'.
import "fmt"
func minOperations(s1 string, s2 string, x int) int {
index := []int{}
for i := range s1 {
if s1[i] != s2[i] {
index = append(index, i)
}
}
n := len(index)
if n & 1 == 1 {
return -1
}
facts := make([][]int, n)
for i := range facts {
facts[i] = make([]int, n)
for j := range facts[i] {
facts[i][j] = -1
}
}
min := func (x, y int) int { if x < y { return x; }; return y; }
var dfs func(i, j int) int
dfs = func(i, j int) int {
if i > j { return 0 }
if facts[i][j] != -1 { return facts[i][j] }
facts[i][j] = dfs(i+1, j-1) + x
facts[i][j] = min(facts[i][j], dfs(i + 2, j) + index[i + 1] - index[i])
facts[i][j] = min(facts[i][j], dfs(i, j - 2) + index[j] - index[j - 1])
return facts[i][j]
}
return dfs(0, n - 1)
}
func minOperations1(s1 string, s2 string, x int) int {
res, odd, pre := 0, 0, 1 << 31
for i := range s1 {
pre += 2
if s1[i] == s2[i] { continue }
odd ^= 1
last := res
res = min(res + x, pre)
pre = last
}
if odd != 0 {
return -1
}
return res / 2
}
func main() {
// Example 1:
// Input: s1 = "1100011000", s2 = "0101001010", x = 2
// Output: 4
// Explanation: We can do the following operations:
// - Choose i = 3 and apply the second operation. The resulting string is s1 = "1101111000".
// - Choose i = 4 and apply the second operation. The resulting string is s1 = "1101001000".
// - Choose i = 0 and j = 8 and apply the first operation. The resulting string is s1 = "0101001010" = s2.
// The total cost is 1 + 1 + 2 = 4. It can be shown that it is the minimum cost possible.
fmt.Println(minOperations("1100011000", "0101001010", 2)) // 4
// Example 2:
// Input: s1 = "10110", s2 = "00011", x = 4
// Output: -1
// Explanation: It is not possible to make the two strings equal.
fmt.Println(minOperations("10110", "00011", 4)) // -1
fmt.Println(minOperations1("1100011000", "0101001010", 2)) // 4
fmt.Println(minOperations1("10110", "00011", 4)) // -1
}