Ctrl K

Nearest Palindrome Number

Hard
You are given a positive integer as a string number. Find the non-negative integer closest to it, other than the number itself, that reads the same left to right and right to left (a palindrome). The distance between two numbers is the absolute value of their difference. If two numbers are equally close, pick the smaller one. Return the answer as a string without leading zeros.
Example 1
number='4071' → '4114'

Two palindromes are nearby: 4004 (67 below) and 4114 (43 above). 4114 is closer.

Example 2
number='99' → '101'

99 itself does not count. 88 and 101 are 11 and 2 away, so the answer is 101.

Example 3
number='100' → '99'

Both 99 and 101 are exactly 1 away from 100. On a tie the smaller one wins: 99.

Constraints
  • 1 ≤ number.length ≤ 30
  • number consists of digits and has no leading zero
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPlus-Minus Calculator With Brackets HardAmount in Words HardSigns Between the Digits HardSimplify a Letter Polynomial HardCalculator With Brackets and Four Operations Hard
loading Python…
Input
number='4071'
Expected
'4114'

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