- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find minimum deletion cost to avoid repeating letters in Python

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

- Related Articles
- Program to find minimum cost to merge stones in Python
- Program to find minimum cost to connect all points in Python
- Program to find minimum cost to cut a stick in Python
- Program to find minimum cost to hire k workers in Python
- Program to find minimum cost for painting houses in Python
- Program to replace all question symbols to avoid consecutive repeating characters in Python
- Program to find out the minimum path to deliver all letters in Python
- Program to Find Out the Minimum Cost to Purchase All in Python
- Program to find minimum total cost for equalizing list elements in Python
- Program to find minimum cost to reduce a list into one integer in Python
- Program to find minimum cost to paint fences with k different colors in Python
- Program to Find Out the Minimum Cost Possible from Weighted Graph in Python
- Program to find minimum cost to pick up gold in given two locations in Python
- Program to find to get minimum cost to climb at the top of stairs in Python?
- Program to find minimum cost to reach final index with at most k steps in python