You are given a string digits made up of decimal digits that contains at least one '5'. Delete exactly one occurrence of the character '5' from digits and return the resulting string whose numeric value is as large as possible.
Function Signature
def max_after_deleting_five(digits: str) -> str:
Rules
-
Exactly one
'5'
is deleted. No other character is removed, added or reordered.
-
Every possible result has length
len(digits) - 1
, so comparing two results numerically is the same as comparing them character by character from the left.
-
The returned string keeps any leading zeros the deletion produces. For example, deleting the only
'5'
from
"503"
returns
"03"
.
-
Different deletions can produce the same string (either
'5'
in
"55"
gives
"5"
). The answer is that string, so the output is always unique.
Constraints
-
2 <= len(digits) <= 100000
-
Every character of
digits
is one of
'0'
through
'9'
.
-
digits[0] != '0'
-
digits
contains at least one
'5'
.
Examples
Example 1
-
Input:
digits = "15958"
-
Output:
"1958"
-
Explanation: Deleting the
'5'
at index 1 gives
"1958"
, and deleting the
'5'
at index 3 gives
"1598"
. The larger value is
"1958"
.
Example 2
-
Input:
digits = "5505"
-
Output:
"550"
-
Explanation: Deleting the
'5'
at index 0 or index 1 gives
"505"
, and deleting the
'5'
at index 3 gives
"550"
, which is larger.
Example 3
-
Input:
digits = "503"
-
Output:
"03"
-
Explanation: There is only one
'5'
. Deleting it leaves
"03"
, and the leading zero is kept.