#1881

Maximum Value after Insertion

expert · 1030 · lc medium +32 · verified · 39.2% accepted · 401 likes · top 18%

Description

Given a very large integer represented as a string n (possibly negative) and a digit x (both in range [1, 9]), insert digit x at any position in n (but not before a negative sign) to produce the numerically largest possible value.

Return the result as a string.

Example 1:

Input: n = "99", x = 9
Output: "999"
Explanation: The result is the same regardless of where you insert 9.

Example 2:

Input: n = "-13", x = 2
Output: "-123"
Explanation: You can make n one of {-213, -123, -132}, and the largest of those three is -123.

Code

1
2
3