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.
How GhostInterview Helps
- Automatically identifies digit positions and applies the greedy choice pattern to maximize the resulting number.
- Generates candidate strings and compares them efficiently, showing the exact reasoning behind the optimal removal.
- Highlights common failure modes and confirms that the final result adheres to the greedy invariant.
Topic Pages
Related GhostInterview Pages
- LeetCode Interview Copilot - Use GhostInterview as a live solver when you want direct help with LeetCode-style coding questions.
- Coding Interview Assistant - See how GhostInterview supports array, string, linked list, graph, and tree interview workflows.
- How GhostInterview Works - Review the screenshot, reasoning, and answer flow before using the solver in a live interview.
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.
Need direct help with Remove Digit From Number to Maximize Result instead of spending more time grinding it?
Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Remove Digit From Number to Maximize Result from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.
Capture the prompt fast instead of rewriting the problem by hand.
Get the solution path, trade-offs, and complexity summary in one pass.
Stay outside captured layers on supported screen-share workflows.
Stay in the same pattern family
Find the longest subsequence repeated k times in a string using backtracking search with pruning.
Open problem page#2800 Shortest String That Contains Three StringsFind the shortest string containing three given strings using a greedy approach while ensuring it is lexicographically smallest.
Open problem page#2844 Minimum Operations to Make a Special NumberMinimize operations to make a number divisible by 25 by deleting digits. Use greedy choice and invariant validation.
Open problem page