Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
87 changes: 66 additions & 21 deletions solution/2900-2999/2976.Minimum Cost to Convert String I/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ tags:
<strong>输入:</strong>source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2]
<strong>输出:</strong>12
<strong>解释:</strong>要将字符 'a' 更改为 'b':
- 将字符 'a' 更改为 'c',成本为 1
- 将字符 'c' 更改为 'b',成本为 2
- 将字符 'a' 更改为 'c',成本为 1
- 将字符 'c' 更改为 'b',成本为 2
产生的总成本是 1 + 2 = 3。
将所有 'a' 更改为 'b',产生的总成本是 3 * 4 = 12 。
</pre>
Expand Down Expand Up @@ -276,39 +276,84 @@ function minimumCost(
changed: string[],
cost: number[],
): number {
const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(Infinity));
for (let i = 0; i < 26; ++i) {
g[i][i] = 0;
}
for (let i = 0; i < original.length; ++i) {
let x: number = original[i].charCodeAt(0) - 'a'.charCodeAt(0);
let y: number = changed[i].charCodeAt(0) - 'a'.charCodeAt(0);
let z: number = cost[i];
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; j < 26; ++j) {
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
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: number = 0;
let n: number = source.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
let x: number = source.charCodeAt(i) - 'a'.charCodeAt(0);
let y: number = target.charCodeAt(i) - 'a'.charCodeAt(0);
if (x !== y) {
if (g[x][y] >= Infinity) {
return -1;
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;
}
```

#### JavaScript

```js
/**
* @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]);
}
}
ans += g[x][y];
}
}

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;
}
};
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,39 +268,84 @@ function minimumCost(
changed: string[],
cost: number[],
): number {
const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(Infinity));
for (let i = 0; i < 26; ++i) {
g[i][i] = 0;
}
for (let i = 0; i < original.length; ++i) {
let x: number = original[i].charCodeAt(0) - 'a'.charCodeAt(0);
let y: number = changed[i].charCodeAt(0) - 'a'.charCodeAt(0);
let z: number = cost[i];
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; j < 26; ++j) {
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
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: number = 0;
let n: number = source.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
let x: number = source.charCodeAt(i) - 'a'.charCodeAt(0);
let y: number = target.charCodeAt(i) - 'a'.charCodeAt(0);
if (x !== y) {
if (g[x][y] >= Infinity) {
return -1;
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;
}
```

#### JavaScript

```js
/**
* @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]);
}
}
ans += g[x][y];
}
}

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;
}
};
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,35 @@ function minimumCost(
changed: string[],
cost: number[],
): number {
const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(Infinity));
for (let i = 0; i < 26; ++i) {
g[i][i] = 0;
}
for (let i = 0; i < original.length; ++i) {
let x: number = original[i].charCodeAt(0) - 'a'.charCodeAt(0);
let y: number = changed[i].charCodeAt(0) - 'a'.charCodeAt(0);
let z: number = cost[i];
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; j < 26; ++j) {
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
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: number = 0;
let n: number = source.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
let x: number = source.charCodeAt(i) - 'a'.charCodeAt(0);
let y: number = target.charCodeAt(i) - 'a'.charCodeAt(0);
if (x !== y) {
if (g[x][y] >= Infinity) {
return -1;
}
ans += g[x][y];
}
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;
}