Ctrl K

Biggest Palindrome From Digits

Medium
You are given a string digits. Take some of its digits (at least one, each at most once) and arrange them in any order to form a palindromic number, one that reads the same left to right and right to left. The number must not start with a zero (the number "0" itself is allowed). Return, as a string, the largest palindrome you can build this way.
Example 1
digits="5283582" → '8523258'

There are pairs of 2s, 5s and 8s plus a spare 3: 8523258 uses all seven digits.

Example 2
digits="0070" → '7'

The palindrome 070 would start with a zero, so the best answer is a lone 7.

Example 3
digits="1203" → '3'

There are no pairs, so the palindrome is a single digit, and the largest is 3.

Constraints
  • 1 ≤ len(digits) ≤ 10⁵
  • digits consists of the digits 0–9 only
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNo Twin Letters Side by Side MediumSplit Letters Into Palindromes MediumPalindrome From Two-Letter Cards MediumLargest String With a Run Cap MediumNine-Button Layout Medium
loading Python…
Input
digits="5283582"
Expected
'8523258'

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