Minimum Operations to Make a Special Number - Problem

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.

Input & Output

Example 1 — Basic Pattern
$ Input: num = "2245047"
Output: 2
💡 Note: We can form "2250" by deleting the digits '4' and '7'. Since 2250 % 25 = 0, we need 2 operations.
Example 2 — Single Zero
$ Input: num = "2908305"
Output: 3
💡 Note: We can form "2900" by deleting '8', '3', '5'. Since 2900 % 25 = 0, we need 3 operations.
Example 3 — Already Special
$ Input: num = "10"
Output: 1
💡 Note: 10 is not divisible by 25. We delete '1' to get '0', and 0 % 25 = 0, so we need 1 operation.

Constraints

  • 1 ≤ num.length ≤ 105
  • num consists of only digits
  • num does not contain any leading zeros

Visualization

Tap to expand
Make Number Special (Divisible by 25)Input: "2245047"Need to make divisible by 25Output: 2 operationsDelete '4' and '7'TransformValid endings for divisibility by 25:00255075"2245047" → "2250"Keep '2', '2', '5', '0' → Delete '4', '7'Result: 2 deletions needed
Understanding the Visualization
1
Input
String num representing a non-negative integer
2
Process
Find pattern ending (00, 25, 50, 75) with minimum deletions
3
Output
Minimum operations needed
Key Takeaway
🎯 Key Insight: Numbers divisible by 25 must end in 00, 25, 50, or 75 - find these patterns greedily
Asked in
Google 23 Amazon 18 Microsoft 15
28.5K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen