Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 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.
Algorithm Approach
To solve this, we will follow these steps ?
- Initialize total_cost := 0
- Iterate through the string from index 1 to end
- For each consecutive group of identical characters, keep the one with maximum cost and delete others
- Add the deletion costs to total_cost
- Return total_cost
Implementation
def solve(s, cost):
total_cost = 0
for i in range(1, len(s)):
# If current character is same as previous character
if s[i] == s[i-1]:
# Delete the character with smaller cost
if cost[i] < cost[i-1]:
total_cost += cost[i]
cost[i] = cost[i-1] # Keep the higher cost for next comparison
else:
total_cost += cost[i-1]
return total_cost
# Test the function
s = "pptpp"
cost = [2, 3, 4, 5, 2]
result = solve(s, cost)
print(f"Minimum deletion cost: {result}")
Minimum deletion cost: 4
How It Works
Let's trace through the example step by step:
def solve_with_trace(s, cost):
print(f"String: {s}")
print(f"Costs: {cost}")
print("Processing:")
total_cost = 0
cost_copy = cost.copy() # Work with a copy to show changes
for i in range(1, len(s)):
if s[i] == s[i-1]:
print(f" Position {i}: '{s[i]}' == '{s[i-1]}'")
if cost_copy[i] < cost_copy[i-1]:
print(f" Delete character at position {i} (cost: {cost_copy[i]})")
total_cost += cost_copy[i]
cost_copy[i] = cost_copy[i-1]
else:
print(f" Delete character at position {i-1} (cost: {cost_copy[i-1]})")
total_cost += cost_copy[i-1]
print(f"Total minimum cost: {total_cost}")
return total_cost
s = "pptpp"
cost = [2, 3, 4, 5, 2]
solve_with_trace(s, cost)
String: pptpp
Costs: [2, 3, 4, 5, 2]
Processing:
Position 1: 'p' == 'p'
Delete character at position 0 (cost: 2)
Position 3: 'p' == 't'
Position 4: 'p' == 'p'
Delete character at position 4 (cost: 2)
Total minimum cost: 4
Alternative Approach Using Grouping
Another way to solve this problem is by grouping consecutive identical characters and keeping only the most expensive one in each group ?
def solve_grouping(s, cost):
if not s:
return 0
total_cost = 0
i = 0
while i < len(s):
j = i
group_costs = []
# Find all consecutive identical characters
while j < len(s) and s[j] == s[i]:
group_costs.append(cost[j])
j += 1
# If group has more than one character, delete all except the most expensive
if len(group_costs) > 1:
total_cost += sum(group_costs) - max(group_costs)
i = j
return total_cost
# Test both approaches
s = "pptpp"
cost = [2, 3, 4, 5, 2]
print(f"Method 1 result: {solve(s, cost)}")
print(f"Method 2 result: {solve_grouping(s, cost)}")
Method 1 result: 4 Method 2 result: 4
Conclusion
Both approaches solve the problem by identifying consecutive identical characters and removing all but the most expensive one from each group. The first method processes characters sequentially, while the second explicitly groups consecutive identical characters before processing.
