#564

Find the Closest Palindrome

hard · verified · 31.9% accepted · 1,322 likes · top 9%

math · string

⊣ practice⊣ quiz⊣ open on leetcode ↗

Description

Given a string n representing an integer, return the closest integer (not including itself), which is a palindrome. If there is a tie, return the smaller one.

The closest is defined as the absolute difference minimized between two integers.

Example 1:

Input: n = "123"
Output: "121"

Example 2:

Input: n = "1"
Output: "0"
Explanation: 0 and 2 are the closest palindromes but we return the smallest which is 0.

Solution