Remove Digit From Number to Maximize Result - Problem
Remove Digit From Number to Maximize Result

Imagine you have a large number as a string, and you need to strategically remove exactly one specific digit to create the largest possible number. This is like editing a phone number or account ID where removing one digit in the right position can make a huge difference!

๐ŸŽฏ Your Goal: Given a string number representing a positive integer and a character digit, remove exactly one occurrence of digit to maximize the resulting number's value.

๐Ÿ“ Key Points:
โ€ข You must remove exactly one occurrence of the specified digit
โ€ข The goal is to maximize the decimal value of the result
โ€ข The digit is guaranteed to exist in the number
โ€ข Return the result as a string

Example: If number = "123" and digit = '3', removing the '3' gives "12". But if number = "1321" and digit = '1', you could remove the first '1' to get "321" or the last '1' to get "132" - choose "321" since it's larger!

Input & Output

example_1.py โ€” Basic Case
$ Input: number = "123", digit = "3"
โ€บ Output: "12"
๐Ÿ’ก Note: There's only one occurrence of '3', so we remove it to get "12"
example_2.py โ€” Multiple Occurrences
$ Input: number = "1231", digit = "1"
โ€บ Output: "231"
๐Ÿ’ก Note: We can remove '1' from position 0 to get "231" or from position 3 to get "123". Since "231" > "123", we return "231"
example_3.py โ€” Greedy Choice
$ Input: number = "551", digit = "5"
โ€บ Output: "51"
๐Ÿ’ก Note: Remove the first '5' because the next digit '5' is not larger. Continue to second '5' where next digit '1' is smaller. Remove the last '5' to get "51"

Visualization

Tap to expand
๐Ÿท๏ธ Strategic Price Tag EditingGoal: Remove one '1' to maximize priceOriginal Price Tag$ 1,321Option 1: Remove first '1'Result$ 321Option 2: Remove last '1'Result$ 132๐Ÿ’ก Greedy StrategyWhen we remove the first '1':โ€ข '3' moves to first positionโ€ข Larger digit in more significant positionโ€ข Results in larger overall number$321 > $132 โœ“
Understanding the Visualization
1
Identify Target
Find the digit that needs to be removed from the number string
2
Greedy Strategy
Look for positions where removing the digit allows a larger digit to move forward
3
Make Decision
Remove at the first beneficial position, or remove the last occurrence if none found
Key Takeaway
๐ŸŽฏ Key Insight: Remove a digit when it blocks a larger digit from moving to a more significant position, maximizing the overall value!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through string, each character examined once

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only storing indices and creating one result string

n
2n
โœ“ Linear Space

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
Asked in
Amazon 25 Google 18 Microsoft 15 Meta 12
26.8K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen