Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Last active December 13, 2023 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save primaryobjects/8f1f9549e4fb427a73e01bcb42221d4c to your computer and use it in GitHub Desktop.
Save primaryobjects/8f1f9549e4fb427a73e01bcb42221d4c to your computer and use it in GitHub Desktop.
const removeDigit = (number, digit) => {
let max = 0;
index = number.indexOf(digit);
while (index != -1) {
left = number.substring(0, index);
right = number.substring(index+1);
value = left + right;
const n = BigInt(value);
max = n > max ? n : max;
index = number.indexOf(digit, index + 1);
}
return max.toString();
};
class Solution:
def removeDigit(self, number: str, digit: str) -> str:
max = 0
index = number.find(digit)
while index != -1:
# Append the left and right to get a new number.
left = number[0:index]
right = number[index+1:len(number)]
value = left + right
max = int(value) if int(value) > max else max
# Get next index.
index = number.find(digit, index+1)
return str(max)
2259. Remove Digit From Number to Maximize Result
Solved
Easy
Topics
Companies
Hint
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".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment