Largest Palindromic Number - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
num = "432"
›
Output:
"4"
💡 Note:
We can form palindromes: "4", "3", "2". The largest is "4".
Example 2 — Multiple Digits
$
Input:
num = "432132"
›
Output:
"432234"
💡 Note:
Count: 4×1, 3×2, 2×2, 1×1. Build first half with pairs: "43", middle: "2", result: "432" + "2" + "234" = "432234".
Example 3 — Leading Zero Edge Case
$
Input:
num = "10"
›
Output:
"1"
💡 Note:
Cannot form "101" due to no leading zeros rule. Best palindrome using available digits is "1".
Constraints
- 1 ≤ num.length ≤ 105
- num consists of digits only
Visualization
Tap to expand
Understanding the Visualization
1
Input
String of digits that can be rearranged
2
Process
Count frequencies and build palindrome greedily
3
Output
Largest palindromic number without leading zeros
Key Takeaway
🎯 Key Insight: Palindromes are symmetric - build greedily from largest digits outward
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code