FastPrepRemove One Five for the Largest Number

Remove One Five for the Largest Number

Cohere logoCohere● EasyNEW GRADOA
Learn

Problem statement

Given the canonical decimal representation of an integer as a string number, remove exactly one occurrence of the digit 5 and return the canonical decimal representation of the largest integer obtainable.

The input may begin with -. Strip leading zeros from the result, and return 0 when every remaining digit is zero or no digit remains.

Function

removeOneFive(number: String) → String

Examples

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"

Among negative candidates, the one with the smallest magnitude is largest.

Constraints

  • 1 <= number.length <= 17.
  • The input is a valid canonical decimal integer and contains at least one digit 5.
  • The result fits in a signed 64-bit integer.
See Cohere hiring insights
public String removeOneFive(String number) {
    // Remove exactly one digit 5 and return the largest integer.
}
number"5456"
expected"546"
Checking account…