304 North Cardinal St.
Dorchester Center, MA 02124

# Remove Digit From Number to Maximize Result LeetCode Solution

## Problem – Remove Digit From Number to Maximize Result LeetCode Solution

You are given a string `number` representing a positive integer and a character `digit`.

Return the resulting string after removing exactly one occurrence of `digit` from `number` such that the value of the resulting string in decimal form is maximized. The test cases are generated such that `digit` occurs at least once in `number`.

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 <= 100`
• `number` consists of digits from `'1'` to `'9'`.
• `digit` is a digit from `'1'` to `'9'`.
• `digit` occurs at least once in `number`.

### Remove Digit From Number to Maximize Result LeetCode Solution in C++

``````string removeDigit(string n, char digit) {
for (int i = 0; i < n.size() - 1; ++i)
if (n[i] == digit && n[i + 1] > digit)
return n.substr(0, i) + n.substr(i + 1);
int last_d = n.rfind(digit);
return n.substr(0, last_d) + n.substr(last_d + 1);
}
``````

### Remove Digit From Number to Maximize Result LeetCode Solution in Java

``````class Solution {
public String removeDigit(String number, char digit) {
List<String> digits = new ArrayList<>();
for (int i = 0; i < number.length(); i++) {
if (number.charAt(i) == digit) {
String stringWithoutDigit = number.substring(0, i) + number.substring(i + 1);
}
}
Collections.sort(digits);
return digits.get(digits.size() - 1);
}
}
``````

### Remove Digit From Number to Maximize Result LeetCode Solution in Python

``````class Solution:
def removeDigit(self, number: str, digit: str) -> str:

# Initializing the last index as zero
last_index = 0

#iterating each number to find the occurences, \
# and to find if the number is greater than the next element \

for num in range(1, len(number)):

# Handling [case 1] and [case 2]
if number[num-1] == digit:
if int(number[num]) > int(number[num-1]):
return number[:num-1] + number[num:]
else:
last_index = num - 1

# If digit is the last number (last occurence) in the string [case 3]
if number[-1] == digit:
last_index = len(number) - 1

return number[:last_index] + number[last_index + 1:]
``````
##### Remove Digit From Number to Maximize Result LeetCode Solution Review:

In our experience, we suggest you solve this Remove Digit From Number to Maximize Result LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Remove Digit From Number to Maximize Result LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Remove Digit From Number to Maximize Result LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions