LeetCode Problem Workspace
Remove Digit From Number to Maximize Result
Determine the largest possible number by removing one specific digit using a greedy approach and string iteration.
3
Topics
6
Code langs
3
Related
Practice Focus
Easy · Greedy choice plus invariant validation
Answer-first summary
Determine the largest possible number by removing one specific digit using a greedy approach and string iteration.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Greedy choice plus invariant validation
To maximize the number after removing one occurrence of a given digit, iterate through all positions where the digit occurs and remove the one that leads to the largest resulting string. A greedy strategy ensures we select the first removal that increases the numeric value, validated by comparing adjacent options. This approach handles all cases efficiently due to the small maximum input size.
Problem Statement
You are given a string number representing a positive integer and a single character digit. Your task is to remove exactly one occurrence of the given digit from number so that the resulting value, interpreted as a decimal integer, is maximized. Each input guarantees that the digit appears at least once in the number.
Return the string representing the maximum number achievable by this single removal. For example, given number = "1231" and digit = "1", removing the first '1' produces "231" while removing the second '1' produces "123". Since 231 is greater than 123, the correct result is "231".
Examples
Example 1
Input: number = "123", digit = "3"
Output: "12"
There is only one '3' in "123". After removing '3', the result is "12".
Example 2
Input: number = "1231", digit = "1"
Output: "231"
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"
We can remove either the first or second '5' from "551". Both result in the string "51".
Constraints
- 2 <= number.length <= 100
- number consists of digits from '1' to '9'.
- digit is a digit from '1' to '9'.
- digit occurs at least once in number.
Solution Approach
Identify all digit positions
Scan the string from left to right and record every index where the target digit occurs. This ensures we consider each potential removal without missing any candidate.
Apply greedy removal
Iterate over recorded indices and remove each occurrence of the digit one at a time, forming candidate strings. Use a greedy comparison: if removing the current digit produces a string greater than all previously observed, update the result.
Return the maximum result
After testing all occurrences, return the candidate string that yields the largest decimal value. This final comparison guarantees the invariant that no other single-digit removal can produce a higher number.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
Time complexity is O(n) because each digit position is checked and candidates are compared in linear time. Space complexity is O(n) for storing positions and temporary candidate strings, where n is the length of number.
What Interviewers Usually Probe
- Checks if you recognize the greedy pattern for maximizing after single removal.
- Observes whether you validate all possible positions of the target digit.
- Wants to see that you efficiently handle comparisons without unnecessary conversions.
Common Pitfalls or Variants
Common pitfalls
- Removing the first occurrence without comparing other positions can fail on numbers like "1231" with digit '1'.
- Comparing string values lexicographically without considering numeric meaning may lead to incorrect selection.
- Not handling multiple consecutive target digits properly can miss the optimal removal.
Follow-up variants
- Remove two digits to maximize result, extending greedy comparisons to pairs of removals.
- Minimize the number instead of maximizing, reversing the greedy logic to remove the largest digit first.
- Allow multiple different digits to be removed, requiring combination enumeration and invariant validation.
FAQ
What is the core strategy for Remove Digit From Number to Maximize Result?
The core strategy is a greedy approach: iterate through all occurrences of the given digit and remove the one that results in the largest number.
Can I just remove the first occurrence of the digit?
No, removing the first occurrence may not always yield the maximum value. You must check all positions where the digit appears.
Does this problem require numeric conversion of the string?
Not necessarily. You can compare candidate strings lexicographically since all strings are of equal or shorter length and contain only digits.
How does the greedy choice ensure the correct result?
By always selecting the removal that produces the largest immediate candidate, we maintain the invariant that no other single removal produces a higher number.
What is the time complexity for this problem?
Time complexity is O(n), where n is the length of the number string, since we examine each occurrence of the target digit once.
Solution
Solution 1: Brute Force Enumeration
We can enumerate all positions $\textit{i}$ in the string $\textit{number}$. If $\textit{number}[i] = \textit{digit}$, we take the prefix $\textit{number}[0:i]$ and the suffix $\textit{number}[i+1:]$ of $\textit{number}$ and concatenate them. This gives the result after removing $\textit{number}[i]$. We then take the maximum of all possible results.
class Solution:
def removeDigit(self, number: str, digit: str) -> str:
return max(
number[:i] + number[i + 1 :] for i, d in enumerate(number) if d == digit
)Solution 2: Greedy
We can enumerate all positions $\textit{i}$ in the string $\textit{number}$. If $\textit{number}[i] = \textit{digit}$, we record the last occurrence position of $\textit{digit}$ as $\textit{last}$. If $\textit{i} + 1 < \textit{n}$ and $\textit{number}[i] < \textit{number}[i + 1]$, then we can directly return $\textit{number}[0:i] + \textit{number}[i+1:]$ as the result after removing $\textit{number}[i]$. This is because if $\textit{number}[i] < \textit{number}[i + 1]$, removing $\textit{number}[i]$ will result in a larger number.
class Solution:
def removeDigit(self, number: str, digit: str) -> str:
return max(
number[:i] + number[i + 1 :] for i, d in enumerate(number) if d == digit
)Continue Topic
string
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Greedy choice plus invariant validation
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward