function minCost(s: string, cost: number[]): number {
let result: number = 0,
rightIndex: number = 1;
while (rightIndex < s.length) {
let maxCost: number = 0,
minCost: number = 0;
while (rightIndex < s.length && s[rightIndex - 1] === s[rightIndex]) {
maxCost = Math.max(maxCost, cost[rightIndex - 1], cost[rightIndex]);
minCost && (minCost -= cost[rightIndex - 1]);
minCost += cost[rightIndex - 1] + cost[rightIndex];
++rightIndex;
}
minCost -= maxCost;
result += minCost;
++rightIndex;
}
return result;
};