Skip to content

Commit 4e02906

Browse files
committed
feat: solve No.2976
1 parent 7939090 commit 4e02906

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
# 2976. Minimum Cost to Convert String I
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, String, Graph, Shortest Path.
5+
- Similar Questions: Can Convert String in K Moves, Minimum Moves to Convert String.
6+
7+
## Problem
8+
9+
You are given two **0-indexed** strings `source` and `target`, both of length `n` and consisting of **lowercase** English letters. You are also given two **0-indexed** character arrays `original` and `changed`, and an integer array `cost`, where `cost[i]` represents the cost of changing the character `original[i]` to the character `changed[i]`.
10+
11+
You start with the string `source`. In one operation, you can pick a character `x` from the string and change it to the character `y` at a cost of `z` **if** there exists **any** index `j` such that `cost[j] == z`, `original[j] == x`, and `changed[j] == y`.
12+
13+
Return **the **minimum** cost to convert the string **`source`** to the string **`target`** using **any** number of operations. If it is impossible to convert** `source` **to** `target`, **return** `-1`.
14+
15+
**Note** that there may exist indices `i`, `j` such that `original[j] == original[i]` and `changed[j] == changed[i]`.
16+
17+
 
18+
Example 1:
19+
20+
```
21+
Input: source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20]
22+
Output: 28
23+
Explanation: To convert the string "abcd" to string "acbe":
24+
- Change value at index 1 from 'b' to 'c' at a cost of 5.
25+
- Change value at index 2 from 'c' to 'e' at a cost of 1.
26+
- Change value at index 2 from 'e' to 'b' at a cost of 2.
27+
- Change value at index 3 from 'd' to 'e' at a cost of 20.
28+
The total cost incurred is 5 + 1 + 2 + 20 = 28.
29+
It can be shown that this is the minimum possible cost.
30+
```
31+
32+
Example 2:
33+
34+
```
35+
Input: source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2]
36+
Output: 12
37+
Explanation: To change the character 'a' to 'b' change the character 'a' to 'c' at a cost of 1, followed by changing the character 'c' to 'b' at a cost of 2, for a total cost of 1 + 2 = 3. To change all occurrences of 'a' to 'b', a total cost of 3 * 4 = 12 is incurred.
38+
```
39+
40+
Example 3:
41+
42+
```
43+
Input: source = "abcd", target = "abce", original = ["a"], changed = ["e"], cost = [10000]
44+
Output: -1
45+
Explanation: It is impossible to convert source to target because the value at index 3 cannot be changed from 'd' to 'e'.
46+
```
47+
48+
 
49+
**Constraints:**
50+
51+
52+
53+
- `1 <= source.length == target.length <= 105`
54+
55+
- `source`, `target` consist of lowercase English letters.
56+
57+
- `1 <= cost.length == original.length == changed.length <= 2000`
58+
59+
- `original[i]`, `changed[i]` are lowercase English letters.
60+
61+
- `1 <= cost[i] <= 106`
62+
63+
- `original[i] != changed[i]`
64+
65+
66+
67+
## Solution
68+
69+
```javascript
70+
/**
71+
* @param {string} source
72+
* @param {string} target
73+
* @param {character[]} original
74+
* @param {character[]} changed
75+
* @param {number[]} cost
76+
* @return {number}
77+
*/
78+
var minimumCost = function(source, target, original, changed, cost) {
79+
var adjacentMap = {};
80+
for (var i = 0; i < cost.length; i++) {
81+
adjacentMap[original[i]] = adjacentMap[original[i]] || {};
82+
adjacentMap[original[i]][changed[i]] = Math.min(
83+
adjacentMap[original[i]][changed[i]] || Number.MAX_SAFE_INTEGER,
84+
cost[i],
85+
);
86+
}
87+
var res = 0;
88+
var minCostMap = {};
89+
for (var j = 0; j < source.length; j++) {
90+
minCostMap[source[j]] = minCostMap[source[j]] || dijkstra(source[j], adjacentMap);
91+
var costMap = minCostMap[source[j]];
92+
if (costMap[target[j]] === undefined) return -1;
93+
res += costMap[target[j]];
94+
}
95+
return res;
96+
};
97+
98+
var dijkstra = function(s, adjacentMap) {
99+
var queue = new MinPriorityQueue();
100+
var minCostMap = {};
101+
queue.enqueue(s, 0);
102+
while (queue.size()) {
103+
var { element, priority } = queue.dequeue();
104+
if (minCostMap[element] <= priority) continue;
105+
minCostMap[element] = priority;
106+
var arr = Object.keys(adjacentMap[element] || {});
107+
for (var i = 0; i < arr.length; i++) {
108+
queue.enqueue(arr[i], priority + adjacentMap[element][arr[i]]);
109+
}
110+
}
111+
return minCostMap;
112+
};
113+
```
114+
115+
**Explain:**
116+
117+
nope.
118+
119+
**Complexity:**
120+
121+
* Time complexity : O(m * n).
122+
* Space complexity : O(m).
123+
124+
## Solution 2
125+
126+
```javascript
127+
/**
128+
* @param {string} source
129+
* @param {string} target
130+
* @param {character[]} original
131+
* @param {character[]} changed
132+
* @param {number[]} cost
133+
* @return {number}
134+
*/
135+
var minimumCost = function(source, target, original, changed, cost) {
136+
var minCostMap = Array(26).fill(0).map(() => Array(26).fill(Number.MAX_SAFE_INTEGER));
137+
var a = 'a'.charCodeAt(0);
138+
for (var i = 0; i < cost.length; i++) {
139+
var o = original[i].charCodeAt(0) - a;
140+
var c = changed[i].charCodeAt(0) - a;
141+
minCostMap[o][c] = Math.min(
142+
minCostMap[o][c],
143+
cost[i],
144+
);
145+
}
146+
for (var k = 0; k < 26; k++) {
147+
for (var i = 0; i < 26; i++) {
148+
for (var j = 0; j < 26; j++) {
149+
minCostMap[i][j] = Math.min(
150+
minCostMap[i][j],
151+
minCostMap[i][k] + minCostMap[k][j],
152+
);
153+
}
154+
}
155+
}
156+
var res = 0;
157+
for (var j = 0; j < source.length; j++) {
158+
var s = source[j].charCodeAt(0) - a;
159+
var t = target[j].charCodeAt(0) - a;
160+
if (s === t) continue;
161+
if (minCostMap[s][t] === Number.MAX_SAFE_INTEGER) return -1;
162+
res += minCostMap[s][t];
163+
}
164+
return res;
165+
};
166+
```
167+
168+
**Explain:**
169+
170+
nope.
171+
172+
**Complexity:**
173+
174+
* Time complexity : O(m * n).
175+
* Space complexity : O(1).
176+

0 commit comments

Comments
 (0)