You are given a string number representing a positive integer and a character digit.
Return _the resulting string after removingexactly one occurrence of _digitfromnumbersuch that the value of the resulting string indecimal form is maximized. The test cases are generated such that
digit occurs at least once in number.
Input: number ="1231", digit ="1"Output: "231"Explanation: We can remove the first '1' to get "231" or remove the second '1' to get "123".Since 231>123, we return"231".
Iterate through the string. For each occurrence of the digit, remove it and compare the resulting string to the current maximum. Return the maximum found.
#include<string>usingnamespace std;
classSolution {
public: string removeDigit(string number, char digit) {
string res ="";
for (int i =0; i < number.size(); ++i) {
if (number[i] == digit) {
string t = number.substr(0, i) + number.substr(i +1);
if (t > res) res = t;
}
}
return res;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
funcremoveDigit(numberstring, digitbyte) string {
res:=""fori:=0; i < len(number); i++ {
ifnumber[i] ==digit {
t:=number[:i] +number[i+1:]
ift > res {
res = t }
}
}
returnres}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
public String removeDigit(String number, char digit) {
String res ="";
for (int i = 0; i < number.length(); ++i) {
if (number.charAt(i) == digit) {
String t = number.substring(0, i) + number.substring(i + 1);
if (t.compareTo(res) > 0) res = t;
}
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution {
funremoveDigit(number: String, digit: Char): String {
var res = ""for (i in number.indices) {
if (number[i] == digit) {
val t = number.removeRange(i, i + 1)
if (t > res) res = t
}
}
return res
}
}
1
2
3
4
5
6
7
8
9
classSolution:
defremoveDigit(self, number: str, digit: str) -> str:
res =""for i, ch in enumerate(number):
if ch == digit:
t = number[:i] + number[i+1:]
if t > res:
res = t
return res
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
impl Solution {
pubfnremove_digit(number: String, digit: char) -> String {
letmut res = String::new();
let chars: Vec<char>= number.chars().collect();
for i in0..chars.len() {
if chars[i] == digit {
let t: String = chars[..i].iter().chain(chars[i+1..].iter()).collect();
if t > res {
res = t;
}
}
}
res
}
}
1
2
3
4
5
6
7
8
9
10
functionremoveDigit(number:string, digit: string):string {
letres="";
for (leti=0; i<number.length; ++i) {
if (number[i] ===digit) {
constt=number.slice(0, i) +number.slice(i+1);
if (t>res) res=t;
}
}
returnres;
}