Suppose we have a string s and another array of integers called cost where cost[i] represents the cost of deleting the ith character in s. We have to find the minimum cost of deletions such that there are no two same letters next to each other. We have to keep in mind that that we will delete the chosen characters at the same time. So after deleting a character, the costs of deleting other characters will not change.
So, if the input is like s = "pptpp", cost = [2,3,4,5,2], then the output will be 4 because if we remove first and last p with cost 2+2 = 4, then the string will be "ptp" here no two identical characters are present one after another
To solve this, we will follow these steps−
- cost_f := 0
- i := 1
- ind := 0
- for i in range 1 to size of s - 1, do
- cur := s[i], c_cost := cost[i]
- prev := s[i-1], p_cost := cost[i-1]
- if ind is same as 1, then
- prev := prev_i, p_cost := cost_i
- if cur is same as prev, then
- if c_cost >= p_cost, then
- cost_f := cost_f + p_cost
- prev_i := 0, cost_i := 0
- ind := 0
- if c_cost < p_cost, then
- cost_f := cost_f + c_cost
- ind := 1
- prev_i := prev, cost_i := p_cost
- if c_cost >= p_cost, then
- otherwise,
- prev_i := 0, cost_i := 0
- ind := 0
- return cost_f
Example
Let us see the following implementation to get better understanding:
def solve(s, cost): cost_f = 0 i = 1 ind = 0 for i in range(1, len(s)): cur, c_cost = s[i], cost[i] prev, p_cost = s[i-1], cost[i-1] if ind == 1: prev, p_cost = prev_i, cost_i if cur == prev: if c_cost >= p_cost: cost_f += p_cost prev_i, cost_i = 0,0 ind = 0 if c_cost < p_cost: cost_f += c_cost ind = 1 prev_i, cost_i = prev, p_cost else: prev_i, cost_i = 0,0 ind = 0 return cost_f s = "pptpp" cost = [2,3,4,5,2] print(solve(s, cost))
Input
"pptpp", [2,3,4,5,2]
Output
4