Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a string s of length n and an integer array cost of the same length, where cost[i] is the cost to delete the ith character of s.
You may delete any number of characters from s (possibly none), such that the resulting string is non-empty and consists of equal characters.
Return an integer denoting the minimum total deletion cost required.
Example 1:
Input: s = "aabaac", cost = [1,2,3,4,1,10]
Output: 11
Explanation:
Deleting the characters at indices 0, 1, 2, 3, 4 results in the string "c", which consists of equal characters, and the total cost is cost[0] + cost[1] + cost[2] + cost[3] + cost[4] = 1 + 2 + 3 + 4 + 1 = 11.
Example 2:
Input: s = "abc", cost = [10,5,8]
Output: 13
Explanation:
Deleting the characters at indices 1 and 2 results in the string "a", which consists of equal characters, and the total cost is cost[1] + cost[2] = 5 + 8 = 13.
Example 3:
Input: s = "zzzzz", cost = [67,67,67,67,67]
Output: 0
Explanation:
All characters in s are equal, so the deletion cost is 0.
Constraints:
n == s.length == cost.length1 <= n <= 1051 <= cost[i] <= 109s consists of lowercase English letters.Problem summary: You are given a string s of length n and an integer array cost of the same length, where cost[i] is the cost to delete the ith character of s. You may delete any number of characters from s (possibly none), such that the resulting string is non-empty and consists of equal characters. Return an integer denoting the minimum total deletion cost required.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
"aabaac" [1,2,3,4,1,10]
"abc" [10,5,8]
"zzzzz" [67,67,67,67,67]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3784: Minimum Deletion Cost to Make All Characters Equal
class Solution {
public long minCost(String s, int[] cost) {
long tot = 0;
Map<Character, Long> g = new HashMap<>(26);
for (int i = 0; i < cost.length; ++i) {
tot += cost[i];
g.merge(s.charAt(i), (long) cost[i], Long::sum);
}
long ans = tot;
for (long v : g.values()) {
ans = Math.min(ans, tot - v);
}
return ans;
}
}
// Accepted solution for LeetCode #3784: Minimum Deletion Cost to Make All Characters Equal
func minCost(s string, cost []int) int64 {
tot := int64(0)
g := map[byte]int64{}
for i, v := range cost {
tot += int64(v)
g[s[i]] += int64(v)
}
ans := tot
for _, x := range g {
ans = min(ans, tot-x)
}
return ans
}
# Accepted solution for LeetCode #3784: Minimum Deletion Cost to Make All Characters Equal
class Solution:
def minCost(self, s: str, cost: List[int]) -> int:
tot = 0
g = defaultdict(int)
for c, v in zip(s, cost):
tot += v
g[c] += v
return min(tot - x for x in g.values())
// Accepted solution for LeetCode #3784: Minimum Deletion Cost to Make All Characters Equal
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3784: Minimum Deletion Cost to Make All Characters Equal
// class Solution {
// public long minCost(String s, int[] cost) {
// long tot = 0;
// Map<Character, Long> g = new HashMap<>(26);
// for (int i = 0; i < cost.length; ++i) {
// tot += cost[i];
// g.merge(s.charAt(i), (long) cost[i], Long::sum);
// }
// long ans = tot;
// for (long v : g.values()) {
// ans = Math.min(ans, tot - v);
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3784: Minimum Deletion Cost to Make All Characters Equal
function minCost(s: string, cost: number[]): number {
let tot = 0;
const g: Map<string, number> = new Map();
for (let i = 0; i < s.length; i++) {
const c = s[i];
const v = cost[i];
tot += v;
g.set(c, (g.get(c) ?? 0) + v);
}
let ans = tot;
for (const x of g.values()) {
ans = Math.min(ans, tot - x);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.