-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheap-letter-deletion.js
30 lines (26 loc) · 1014 Bytes
/
cheap-letter-deletion.js
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
function solution(S, C) {
let totalCost = 0;
// Iterate through the string one letter at a time
for (let i = 0; i < S.length - 1; i++) {
if (S[i] === S[i + 1]) {
// If two consecutive characters are the same, delete the one with the lesser cost
totalCost += Math.min(C[i], C[i + 1]);
// console.log("Step: " + totalCost);
}
}
return totalCost;
}
// Check if an argument is provided
if (process.argv.length > 2) {
const input = process.argv[2];
if (typeof input === 'string') {
// Assuming the second argument is a comma-separated list of costs
const costs = process.argv[3] ? process.argv[3].split(',').map(Number) : [];
// Call solution with the string and costs array
console.log("Total Cost: " + solution(input, costs));
} else {
console.error('Please provide a valid string as the first argument.');
}
} else {
console.error('Please provide a string and a list of costs as arguments Ex: node cheap-letter-deletion.js "abccbd" "1,2,3"');
}