Ctrl K

Best Spot for a Digit

Medium
You get an integer written as a string number (it may be very long and may be negative, in which case it starts with -) and a digit digit between 1 and 9. Insert digit anywhere in the written number so that the resulting value is as large as possible. The digit cannot go to the left of the minus sign. Return the result as a string.
Example 1
number="7315", digit=4 → '74315'

It pays to place the digit just before the first digit smaller than 4, giving 74315.

Example 2
number="-482", digit=5 → '-4582'

For a negative number the magnitude should be as small as possible: -4582 beats -5482, -4852 and -4825.

Example 3
number="66", digit=6 → '666'
Constraints
  • 1 ≤ len(number) ≤ 10⁵
  • 1 ≤ digit ≤ 9
  • number is a valid integer with no leading zeros
  • A negative number starts with - followed by at least one digit
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Number From Pieces MediumEvery Letter Once MediumErase Digits for the Smallest Number MediumSmallest Up-Down Sequence MediumRing of Flippable Words Medium
loading Python…
Input
number="7315", digit=4
Expected
'74315'

Run checks the first 3 tests in your browser. Submit checks all 127 tests on the server, hidden ones included.