2384. Largest Palindromic Number

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2384

class Solution:
    def largestPalindromic(self, num: str) -> str:
        cnt = Counter(num)
        res = ''
        for i in range(9, -1, -1):
            v = str(i)
            if cnt[v] % 2:
                res = v
                cnt[v] -= 1
                break
        for i in range(10):
            v = str(i)
            if cnt[v]:
                cnt[v] //= 2
                s = cnt[v] * v
                res = s + res + s
        return res.strip('0') or '0'