Largest Palindromic Number

You are given a string num consisting of digits only.

Return the largest palindromic integer, in the form of a string, that can be formed using digits taken from num. It should not contain leading zeroes.

Notes:

  • You do not need to use all the digits of num, but you must use at least one digit.
  • The digits can be reordered.
Example 1
Inputnum = "444947137"
Output"7449447"
Use the digits "4449477" from num to form "7449447", which is the largest palindromic integer that can be formed.
Example 2
Inputnum = "00009"
Output"9"
"9" is the largest palindromic integer that can be formed, and the returned integer should not contain leading zeroes.

Constraints

  • 1 <= num.length <= 10^5
  • num consists of digits.

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate