Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
You are given a 0-indexed string num representing a non-negative integer.
In one operation, you can pick any digit of num and delete it. Note that if you delete all the digits of num, num becomes 0.
Return the minimum number of operations required to make num special.
An integer x is considered special if it is divisible by 25.
Example 1:
Input: num = "2245047" Output: 2 Explanation: Delete digits num[5] and num[6]. The resulting number is "22450" which is special since it is divisible by 25. It can be shown that 2 is the minimum number of operations required to get a special number.
Example 2:
Input: num = "2908305" Output: 3 Explanation: Delete digits num[3], num[4], and num[6]. The resulting number is "2900" which is special since it is divisible by 25. It can be shown that 3 is the minimum number of operations required to get a special number.
Example 3:
Input: num = "10" Output: 1 Explanation: Delete digit num[0]. The resulting number is "0" which is special since it is divisible by 25. It can be shown that 1 is the minimum number of operations required to get a special number.
Constraints:
1 <= num.length <= 100num only consists of digits '0' through '9'.num does not contain any leading zeros.Problem summary: You are given a 0-indexed string num representing a non-negative integer. In one operation, you can pick any digit of num and delete it. Note that if you delete all the digits of num, num becomes 0. Return the minimum number of operations required to make num special. An integer x is considered special if it is divisible by 25.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Greedy
"2245047"
"2908305"
"10"
remove-k-digits)remove-digit-from-number-to-maximize-result)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2844: Minimum Operations to Make a Special Number
class Solution {
private Integer[][] f;
private String num;
private int n;
public int minimumOperations(String num) {
n = num.length();
this.num = num;
f = new Integer[n][25];
return dfs(0, 0);
}
private int dfs(int i, int k) {
if (i == n) {
return k == 0 ? 0 : n;
}
if (f[i][k] != null) {
return f[i][k];
}
f[i][k] = dfs(i + 1, k) + 1;
f[i][k] = Math.min(f[i][k], dfs(i + 1, (k * 10 + num.charAt(i) - '0') % 25));
return f[i][k];
}
}
// Accepted solution for LeetCode #2844: Minimum Operations to Make a Special Number
func minimumOperations(num string) int {
n := len(num)
f := make([][25]int, n)
for i := range f {
for j := range f[i] {
f[i][j] = -1
}
}
var dfs func(i, k int) int
dfs = func(i, k int) int {
if i == n {
if k == 0 {
return 0
}
return n
}
if f[i][k] != -1 {
return f[i][k]
}
f[i][k] = dfs(i+1, k) + 1
f[i][k] = min(f[i][k], dfs(i+1, (k*10+int(num[i]-'0'))%25))
return f[i][k]
}
return dfs(0, 0)
}
# Accepted solution for LeetCode #2844: Minimum Operations to Make a Special Number
class Solution:
def minimumOperations(self, num: str) -> int:
@cache
def dfs(i: int, k: int) -> int:
if i == n:
return 0 if k == 0 else n
ans = dfs(i + 1, k) + 1
ans = min(ans, dfs(i + 1, (k * 10 + int(num[i])) % 25))
return ans
n = len(num)
return dfs(0, 0)
// Accepted solution for LeetCode #2844: Minimum Operations to Make a Special Number
// 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 #2844: Minimum Operations to Make a Special Number
// class Solution {
// private Integer[][] f;
// private String num;
// private int n;
//
// public int minimumOperations(String num) {
// n = num.length();
// this.num = num;
// f = new Integer[n][25];
// return dfs(0, 0);
// }
//
// private int dfs(int i, int k) {
// if (i == n) {
// return k == 0 ? 0 : n;
// }
// if (f[i][k] != null) {
// return f[i][k];
// }
// f[i][k] = dfs(i + 1, k) + 1;
// f[i][k] = Math.min(f[i][k], dfs(i + 1, (k * 10 + num.charAt(i) - '0') % 25));
// return f[i][k];
// }
// }
// Accepted solution for LeetCode #2844: Minimum Operations to Make a Special Number
function minimumOperations(num: string): number {
const n = num.length;
const f: number[][] = Array.from({ length: n }, () => Array.from({ length: 25 }, () => -1));
const dfs = (i: number, k: number): number => {
if (i === n) {
return k === 0 ? 0 : n;
}
if (f[i][k] !== -1) {
return f[i][k];
}
f[i][k] = dfs(i + 1, k) + 1;
f[i][k] = Math.min(f[i][k], dfs(i + 1, (k * 10 + Number(num[i])) % 25));
return f[i][k];
};
return dfs(0, 0);
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.