#2384

Largest Palindromic Number

expert · 1075 · lc medium +32 · verified · 37% accepted · 670 likes · top 15%

Description

Given a string num of digits, form the largest possible palindromic integer using a subset of those digits (reordering allowed). The result must not have leading zeros, and must use at least one digit.

Return the result as a string.

Example 1:

Input: num = "444947137"
Output: "7449447"
Explanation:
Use the digits "4449477" from "444947137" to form the palindromic integer "7449447".
It can be shown that "7449447" is the largest palindromic integer that can be formed.

Example 2:

Input: num = "00009"
Output: "9"
Explanation:
It can be shown that "9" is the largest palindromic integer that can be formed.
Note that the integer returned should not contain leading zeroes.

Code

1
2
3