Remove Digit From Number to Maximize Result
EasyUpdated: Aug 2, 2025
Practice on:
Problem
You are given a string number representing a positive integer and a character digit.
Return _the resulting string after removingexactly one occurrence of _digit fromnumber such 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.
Examples
Example 1
Input: number = "123", digit = "3"
Output: "12"
Explanation: There is only one '3' in "123". After removing '3', the result is "12".
Example 2
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".
Example 3
Input: number = "551", digit = "5"
Output: "51"
Explanation: We can remove either the first or second '5' from "551".
Both result in the string "51".
Constraints
2 <= number.length <= 100numberconsists of digits from'1'to'9'.digitis a digit from'1'to'9'.digitoccurs at least once innumber.
Solution
Method 1 - Try Removing Each Occurrence
Intuition
To maximize the result, try removing each occurrence of the digit and keep the maximum string (by value).
Approach
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.
Code
C++
#include <string>
using namespace std;
class Solution {
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;
}
};
Go
func removeDigit(number string, digit byte) string {
res := ""
for i := 0; i < len(number); i++ {
if number[i] == digit {
t := number[:i] + number[i+1:]
if t > res {
res = t
}
}
}
return res
}
Java
class Solution {
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;
}
}
Kotlin
class Solution {
fun removeDigit(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
}
}
Python
class Solution:
def removeDigit(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
Rust
impl Solution {
pub fn remove_digit(number: String, digit: char) -> String {
let mut res = String::new();
let chars: Vec<char> = number.chars().collect();
for i in 0..chars.len() {
if chars[i] == digit {
let t: String = chars[..i].iter().chain(chars[i+1..].iter()).collect();
if t > res {
res = t;
}
}
}
res
}
}
TypeScript
function removeDigit(number: string, digit: string): string {
let res = "";
for (let i = 0; i < number.length; ++i) {
if (number[i] === digit) {
const t = number.slice(0, i) + number.slice(i + 1);
if (t > res) res = t;
}
}
return res;
}
Complexity
- ⏰ Time complexity:
O(n)where n is the length of number. - 🧺 Space complexity:
O(n)for the result string.