2817. Minimum Cost to Make All Characters Equal
My accepted Python solution to LeetCode problem 2817, Minimum Cost to Make All Characters Equal, running in 102ms.
- Difficulty: Medium
- Python
- Runtime 102ms
- Memory 17.8MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 102ms, memory 17.8MB, accepted 2025-12-30.
class Solution:
def minimumCost(self, s: str) -> int:
n = len(s)
cost = 0
# For each pair of adjacent characters that differ
# We need to flip one side or the other
# Greedy: at position i, if s[i] != s[i+1], flip the smaller side
for i in range(n - 1):
if s[i] != s[i + 1]:
# Cost to flip left side (0 to i) is i + 1
# Cost to flip right side (i+1 to n-1) is n - i - 1
cost += min(i + 1, n - i - 1)
return cost