Input: num ="2245047"Output: 2Explanation: 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 2is the minimum number of operations required to get a special number.
Input: num ="2908305"Output: 3Explanation: 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 3is the minimum number of operations required to get a special number.
Input: num ="10"Output: 1Explanation: Delete digit num[0]. The resulting number is"0" which is special since it is divisible by 25.It can be shown that 1is the minimum number of operations required to get a special number.
To make the number divisible by 25, its last two digits must be 00, 25, 50, or 75. We try to find the minimum number of deletions needed to make the string end with any of these pairs.
classSolution {
public:int minimumOperations(string num) {
vector<string> ends = {"00", "25", "50", "75"};
int n = num.size(), ans = n;
for (auto& e : ends) {
int j = n -1;
while (j >=0&& num[j] != e[1]) --j;
if (j <0) continue;
int i = j -1;
while (i >=0&& num[i] != e[0]) --i;
if (i <0) continue;
ans = min(ans, n - i -2);
}
return ans;
}
};
classSolution {
publicintminimumOperations(String num) {
String[] ends = {"00", "25", "50", "75"};
int n = num.length(), ans = n;
for (String e : ends) {
int j = n - 1;
while (j >= 0 && num.charAt(j) != e.charAt(1)) --j;
if (j < 0) continue;
int i = j - 1;
while (i >= 0 && num.charAt(i) != e.charAt(0)) --i;
if (i < 0) continue;
ans = Math.min(ans, n - i - 2);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
funminimumOperations(num: String): Int {
val ends = listOf("00", "25", "50", "75")
val n = num.length
var ans = n
for (e in ends) {
var j = n - 1while (j >=0&& num[j] != e[1]) j--if (j < 0) continuevar i = j - 1while (i >=0&& num[i] != e[0]) i--if (i < 0) continue ans = minOf(ans, n - i - 2)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
defminimumOperations(self, num: str) -> int:
ends = ["00", "25", "50", "75"]
n = len(num)
ans = n
for e in ends:
j = n -1while j >=0and num[j] != e[1]:
j -=1if j <0:
continue i = j -1while i >=0and num[i] != e[0]:
i -=1if i <0:
continue ans = min(ans, n - i -2)
return ans
impl Solution {
pubfnminimum_operations(num: String) -> i32 {
let ends = ["00", "25", "50", "75"];
let n = num.len();
let num = num.as_bytes();
letmut ans = n asi32;
for e in ends.iter() {
letmut j = n asi32-1;
while j >=0&& num[j asusize] != e.as_bytes()[1] {
j -=1;
}
if j <0 { continue; }
letmut i = j -1;
while i >=0&& num[i asusize] != e.as_bytes()[0] {
i -=1;
}
if i <0 { continue; }
ans = ans.min(n asi32- i -2);
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
minimumOperations(num: string):number {
constends= ["00", "25", "50", "75"];
constn=num.length;
letans=n;
for (consteofends) {
letj=n-1;
while (j>=0&&num[j] !==e[1]) --j;
if (j<0) continue;
leti=j-1;
while (i>=0&&num[i] !==e[0]) --i;
if (i<0) continue;
ans= Math.min(ans, n-i-2);
}
returnans;
}
}