2844. Minimum Operations to Make a Special Number LeetCode Solution

In this guide, you will get 2844. Minimum Operations to Make a Special Number LeetCode Solution with the best time and space complexity. The solution to Minimum Operations to Make a Special Number problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Operations to Make a Special Number solution in C++
  4. Minimum Operations to Make a Special Number solution in Java
  5. Minimum Operations to Make a Special Number solution in Python
  6. Additional Resources
2844. Minimum Operations to Make a Special Number LeetCode Solution image

Problem Statement of Minimum Operations to Make a Special Number

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:

See also  218. The Skyline Problem LeetCode Solution

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 <= 100
num only consists of digits '0' through '9'.
num does not contain any leading zeros.

Complexity Analysis

  • Time Complexity: O(|\texttt{num}|)
  • Space Complexity: O(1)

2844. Minimum Operations to Make a Special Number LeetCode Solution in C++

class Solution {
 public:
  int minimumOperations(string num) {
    const int n = num.length();
    bool seenFive = false;
    bool seenZero = false;

    for (int i = n - 1; i >= 0; --i) {
      if (seenZero && num[i] == '0')  // '00'
        return n - i - 2;
      if (seenZero && num[i] == '5')  // '50'
        return n - i - 2;
      if (seenFive && num[i] == '2')  // '25'
        return n - i - 2;
      if (seenFive && num[i] == '7')  // '75'
        return n - i - 2;
      seenZero = seenZero || num[i] == '0';
      seenFive = seenFive || num[i] == '5';
    }

    return seenZero ? n - 1 : n;
  }
};
/* code provided by PROGIEZ */

2844. Minimum Operations to Make a Special Number LeetCode Solution in Java

class Solution {
  public int minimumOperations(String num) {
    final int n = num.length();
    boolean seenFive = false;
    boolean seenZero = false;

    for (int i = n - 1; i >= 0; --i) {
      if (seenZero && num.charAt(i) == '0') // '00'
        return n - i - 2;
      if (seenZero && num.charAt(i) == '5') // '50'
        return n - i - 2;
      if (seenFive && num.charAt(i) == '2') // '25'
        return n - i - 2;
      if (seenFive && num.charAt(i) == '7') // '75'
        return n - i - 2;
      seenZero = seenZero || num.charAt(i) == '0';
      seenFive = seenFive || num.charAt(i) == '5';
    }

    return seenZero ? n - 1 : n;
  }
}
// code provided by PROGIEZ

2844. Minimum Operations to Make a Special Number LeetCode Solution in Python

class Solution:
  def minimumOperations(self, num: str) -> int:
    n = len(num)
    seenFive = False
    seenZero = False

    for i in range(n - 1, -1, -1):
      if seenZero and num[i] == '0':  # '00'
        return n - i - 2
      if seenZero and num[i] == '5':  # '50'
        return n - i - 2
      if seenFive and num[i] == '2':  # '25'
        return n - i - 2
      if seenFive and num[i] == '7':  # '75'
        return n - i - 2
      seenZero = seenZero or num[i] == '0'
      seenFive = seenFive or num[i] == '5'

    return n - 1 if seenZero else n
# code by PROGIEZ

Additional Resources

See also  2429. Minimize XOR LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.