Minimum Operations to Make a Special Number - Problem

You're given a string num representing a non-negative integer. Your task is to make this number special by deleting as few digits as possible.

A number is considered special if it's divisible by 25. This means the number must end in one of these patterns: 00, 25, 50, or 75.

Operations allowed:

  • Delete any digit from the string
  • If all digits are deleted, the number becomes 0 (which is divisible by 25)

Goal: Return the minimum number of deletions needed to make the number divisible by 25.

Example: For num = "2245047", you can delete "2245047" โ†’ "2250" (delete '4' and '7') to get a number ending in '50', which requires 2 operations.

Input & Output

example_1.py โ€” Basic Pattern Matching
$ Input: num = "2245047"
โ€บ Output: 2
๐Ÿ’ก Note: We can delete digits to form "2250" (ending in 50, divisible by 25). Delete positions with '4' and '7', requiring 2 operations.
example_2.py โ€” Multiple Valid Patterns
$ Input: num = "2908305"
โ€บ Output: 3
๐Ÿ’ก Note: Multiple patterns possible: keep "00" (4 deletions), keep "25" (5 deletions), keep "50" (5 deletions), or make "0" (6 deletions). Best is keeping "00" with 4 deletions... wait, let me recalculate: we can make "2250" by deleting '9','8','3' (3 deletions).
example_3.py โ€” Edge Case: Single Zero
$ Input: num = "0"
โ€บ Output: 0
๐Ÿ’ก Note: The number is already 0, which is divisible by 25. No operations needed.

Constraints

  • 1 โ‰ค num.length โ‰ค 105
  • num consists of only digits
  • num does not contain any leading zeros except for the number 0 itself

Visualization

Tap to expand
Divisibility by 25: Pattern RecognitionValid Ending Patterns:00255075Example: "2245047"2245047Keep "50"DeleteStep 1: Find rightmost '0' โ†’ position 4Step 2: Find rightmost '5' before pos 4 โ†’ position 3Step 3: Keep digits at positions 3,4 โ†’ "50"Result: Delete 5 digits โ†’ 2 operationsPattern Analysis:โ€ข Pattern "00": Need 2 zerosOnly 1 zero available โŒโ€ข Pattern "25": positions 1,3Delete 5 digits โœ“โ€ข Pattern "50": positions 3,4Delete 5 digits โœ“ OPTIMALโ€ข Pattern "75": No '7' before '5'Not possible โŒ๐ŸŽฏ Key Insight: Work backwards to find patterns with minimum deletionsTime: O(n) | Space: O(1)
Understanding the Visualization
1
Identify Target
Numbers divisible by 25 must end in: 00, 25, 50, or 75
2
Work Backwards
Scan from right to find each pattern with minimum deletions
3
Special Case
Consider making the number 0 by keeping just one zero
4
Choose Minimum
Return the pattern requiring the fewest deletions
Key Takeaway
๐ŸŽฏ Key Insight: Since divisibility by 25 requires specific ending patterns, we can work backwards greedily to find the optimal pattern with minimum deletions in linear time.
Asked in
Google 15 Meta 12 Amazon 8 Microsoft 6
23.0K Views
Medium Frequency
~15 min Avg. Time
850 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