comments | difficulty | edit_url | rating | source | tags | ||||
---|---|---|---|---|---|---|---|---|---|
true |
中等 |
1882 |
第 377 场周赛 Q3 |
|
给你两个下标从 0 开始的字符串 source
和 target
,它们的长度均为 n
并且由 小写 英文字母组成。
另给你两个下标从 0 开始的字符数组 original
和 changed
,以及一个整数数组 cost
,其中 cost[i]
代表将字符 original[i]
更改为字符 changed[i]
的成本。
你从字符串 source
开始。在一次操作中,如果 存在 任意 下标 j
满足 cost[j] == z
、original[j] == x
以及 changed[j] == y
。你就可以选择字符串中的一个字符 x
并以 z
的成本将其更改为字符 y
。
返回将字符串 source
转换为字符串 target
所需的 最小 成本。如果不可能完成转换,则返回 -1
。
注意,可能存在下标 i
、j
使得 original[j] == original[i]
且 changed[j] == changed[i]
。
示例 1:
输入: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] 输出:28 解释:将字符串 "abcd" 转换为字符串 "acbe" : - 更改下标 1 处的值 'b' 为 'c' ,成本为 5 。 - 更改下标 2 处的值 'c' 为 'e' ,成本为 1 。 - 更改下标 2 处的值 'e' 为 'b' ,成本为 2 。 - 更改下标 3 处的值 'd' 为 'e' ,成本为 20 。 产生的总成本是 5 + 1 + 2 + 20 = 28 。 可以证明这是可能的最小成本。
示例 2:
输入:source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2] 输出:12 解释:要将字符 'a' 更改为 'b': - 将字符 'a' 更改为 'c',成本为 1 - 将字符 'c' 更改为 'b',成本为 2 产生的总成本是 1 + 2 = 3。 将所有 'a' 更改为 'b',产生的总成本是 3 * 4 = 12 。
示例 3:
输入:source = "abcd", target = "abce", original = ["a"], changed = ["e"], cost = [10000] 输出:-1 解释:无法将 source 字符串转换为 target 字符串,因为下标 3 处的值无法从 'd' 更改为 'e' 。
提示:
1 <= source.length == target.length <= 105
source
、target
均由小写英文字母组成1 <= cost.length== original.length == changed.length <= 2000
original[i]
、changed[i]
是小写英文字母1 <= cost[i] <= 106
original[i] != changed[i]
根据题目描述,我们可以将每个字母看作一个节点,每对字母的转换成本看作一条有向边。那么我们先初始化一个
然后我们遍历数组
接下来,我们使用 Floyd 算法计算出
遍历结束后,返回答案即可。
时间复杂度
class Solution:
def minimumCost(
self,
source: str,
target: str,
original: List[str],
changed: List[str],
cost: List[int],
) -> int:
g = [[inf] * 26 for _ in range(26)]
for i in range(26):
g[i][i] = 0
for x, y, z in zip(original, changed, cost):
x = ord(x) - ord('a')
y = ord(y) - ord('a')
g[x][y] = min(g[x][y], z)
for k in range(26):
for i in range(26):
for j in range(26):
g[i][j] = min(g[i][j], g[i][k] + g[k][j])
ans = 0
for a, b in zip(source, target):
if a != b:
x, y = ord(a) - ord('a'), ord(b) - ord('a')
if g[x][y] >= inf:
return -1
ans += g[x][y]
return ans
class Solution {
public long minimumCost(
String source, String target, char[] original, char[] changed, int[] cost) {
final int inf = 1 << 29;
int[][] g = new int[26][26];
for (int i = 0; i < 26; ++i) {
Arrays.fill(g[i], inf);
g[i][i] = 0;
}
for (int i = 0; i < original.length; ++i) {
int x = original[i] - 'a';
int y = changed[i] - 'a';
int z = cost[i];
g[x][y] = Math.min(g[x][y], z);
}
for (int k = 0; k < 26; ++k) {
for (int i = 0; i < 26; ++i) {
for (int j = 0; j < 26; ++j) {
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
}
}
}
long ans = 0;
int n = source.length();
for (int i = 0; i < n; ++i) {
int x = source.charAt(i) - 'a';
int y = target.charAt(i) - 'a';
if (x != y) {
if (g[x][y] >= inf) {
return -1;
}
ans += g[x][y];
}
}
return ans;
}
}
class Solution {
public:
long long minimumCost(string source, string target, vector<char>& original, vector<char>& changed, vector<int>& cost) {
const int inf = 1 << 29;
int g[26][26];
for (int i = 0; i < 26; ++i) {
fill(begin(g[i]), end(g[i]), inf);
g[i][i] = 0;
}
for (int i = 0; i < original.size(); ++i) {
int x = original[i] - 'a';
int y = changed[i] - 'a';
int z = cost[i];
g[x][y] = min(g[x][y], z);
}
for (int k = 0; k < 26; ++k) {
for (int i = 0; i < 26; ++i) {
for (int j = 0; j < 26; ++j) {
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}
}
}
long long ans = 0;
int n = source.length();
for (int i = 0; i < n; ++i) {
int x = source[i] - 'a';
int y = target[i] - 'a';
if (x != y) {
if (g[x][y] >= inf) {
return -1;
}
ans += g[x][y];
}
}
return ans;
}
};
func minimumCost(source string, target string, original []byte, changed []byte, cost []int) (ans int64) {
const inf = 1 << 29
g := make([][]int, 26)
for i := range g {
g[i] = make([]int, 26)
for j := range g[i] {
if i == j {
g[i][j] = 0
} else {
g[i][j] = inf
}
}
}
for i := 0; i < len(original); i++ {
x := int(original[i] - 'a')
y := int(changed[i] - 'a')
z := cost[i]
g[x][y] = min(g[x][y], z)
}
for k := 0; k < 26; k++ {
for i := 0; i < 26; i++ {
for j := 0; j < 26; j++ {
g[i][j] = min(g[i][j], g[i][k]+g[k][j])
}
}
}
n := len(source)
for i := 0; i < n; i++ {
x := int(source[i] - 'a')
y := int(target[i] - 'a')
if x != y {
if g[x][y] >= inf {
return -1
}
ans += int64(g[x][y])
}
}
return
}
function minimumCost(
source: string,
target: string,
original: string[],
changed: string[],
cost: number[],
): number {
const [n, m, MAX] = [source.length, original.length, Number.POSITIVE_INFINITY];
const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(MAX));
const getIndex = (ch: string) => ch.charCodeAt(0) - 'a'.charCodeAt(0);
for (let i = 0; i < 26; ++i) g[i][i] = 0;
for (let i = 0; i < m; ++i) {
const x = getIndex(original[i]);
const y = getIndex(changed[i]);
const z = cost[i];
g[x][y] = Math.min(g[x][y], z);
}
for (let k = 0; k < 26; ++k) {
for (let i = 0; i < 26; ++i) {
for (let j = 0; g[i][k] < MAX && j < 26; j++) {
if (g[k][j] < MAX) {
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
}
}
}
}
let ans = 0;
for (let i = 0; i < n; ++i) {
const x = getIndex(source[i]);
const y = getIndex(target[i]);
if (x === y) continue;
if (g[x][y] === MAX) return -1;
ans += g[x][y];
}
return ans;
}
/**
* @param {string} source
* @param {string} target
* @param {character[]} original
* @param {character[]} changed
* @param {number[]} cost
* @return {number}
*/
var minimumCost = function (source, target, original, changed, cost) {
const [n, m, MAX] = [source.length, original.length, Number.POSITIVE_INFINITY];
const g = Array.from({ length: 26 }, () => Array(26).fill(MAX));
const getIndex = ch => ch.charCodeAt(0) - 'a'.charCodeAt(0);
for (let i = 0; i < 26; ++i) g[i][i] = 0;
for (let i = 0; i < m; ++i) {
const x = getIndex(original[i]);
const y = getIndex(changed[i]);
const z = cost[i];
g[x][y] = Math.min(g[x][y], z);
}
for (let k = 0; k < 26; ++k) {
for (let i = 0; i < 26; ++i) {
for (let j = 0; g[i][k] < MAX && j < 26; j++) {
if (g[k][j] < MAX) {
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
}
}
}
}
let ans = 0;
for (let i = 0; i < n; ++i) {
const x = getIndex(source[i]);
const y = getIndex(target[i]);
if (x === y) continue;
if (g[x][y] === MAX) return -1;
ans += g[x][y];
}
return ans;
};