Remove One Five for the Largest Number
Updated
Problem statement
You are given an integer written as a string number. Remove exactly one digit 5 from it. Return the largest integer you can get, also written as a string.
The input may begin with -, which means it is negative. Write the result the normal way, with no leading zeros. Return 0 when every remaining digit is zero or no digit remains.
Function
removeOneFive(number: String) → StringExamples
Example 1
number = "5456"return = "546"Removing the first 5 gives 456; removing the second gives 546, which is larger.
Example 2
number = "-5155"return = "-155"The number is negative, so the largest result is the one closest to zero. Removing the first five leaves a number that starts with one. Removing either of the last two fives leaves a number that starts with five, which is further below zero. So the answer comes from removing the first five.
Constraints
1 <= number.length <= 17.numberis a valid integer written the normal way, with no plus sign and no leading zeros. It contains at least one digit5.- The result fits in a signed 64-bit integer.
Similar questions at other companies
- Maximum Ones After K OperationsGoldman Sachs · OAGoldman Sachs interview questions
- Minimum Operations to Reduce a Transaction LedgerVisa · OAVisa interview questions
- Prepare NotificationMicrosoft · OAMicrosoft interview questions
- Minimum Changes for Distinct Adjacent CharactersHeadlands Technologies · OAHeadlands Technologies interview questions
- Largest Number With Even Digit FrequenciesDRW · OADRW interview questions
- Minimum Replacements for Unequal Adjacent CharactersIBM · OAIBM interview questions
- Crop Text at a Whole-Word BoundaryAmerican Express · OAAmerican Express interview questions
- Minimum Flips for Even Binary RunsJP Morgan Chase · OAJP Morgan Chase interview questions
- Missing WordsVanguard · OAVanguard interview questions
- Digit Changes for a Multiple of 9Nutanix · OANutanix interview questions