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:
num
, but you must use at least one digit.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.
Constraints:
1 <= num.length <= 105
num
consists of digits.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to find the biggest palindrome we can create using digits from a given number. The brute force method tries every possible combination of digits to see if it forms a palindrome and then picks the largest one.
Here's how the algorithm would work step-by-step:
def largest_palindrome_brute_force(digits):
largest_palindrome_found = ""
digits_length = len(digits)
for start_index in range(digits_length):
for end_index in range(digits_length, start_index, -1):
substring = digits[start_index:end_index]
# Check if the substring is a palindrome
if substring == substring[::-1]:
# Update if current substring is larger
if len(substring) > len(largest_palindrome_found):
largest_palindrome_found = substring
elif len(substring) == len(largest_palindrome_found) and substring > largest_palindrome_found:
largest_palindrome_found = substring
# Need to handle empty string case to return '0' if no palindromes were found
if not largest_palindrome_found:
return '0'
return largest_palindrome_found
The best way to solve this is to build the largest possible palindrome digit by digit. We achieve this by greedily using the largest digit pairs available and placing them on either end of our result. We only need to handle a possible middle digit as a special case.
Here's how the algorithm would work step-by-step:
def largest_palindromic_number(number):
digit_counts = {}
for digit in number:
digit_counts[digit] = digit_counts.get(digit, 0) + 1
first_half = ""
middle_digit = ""
# Build the first half of the palindrome.
for digit in range(9, -1, -1):
digit_string = str(digit)
count = digit_counts.get(digit_string, 0)
first_half += digit_string * (count // 2)
# Find the largest digit that appears an odd number of times.
for digit in range(9, -1, -1):
digit_string = str(digit)
count = digit_counts.get(digit_string, 0)
if count % 2 == 1:
middle_digit = digit_string
break
second_half = first_half[::-1]
palindrome = first_half + middle_digit + second_half
# Remove leading zeros, but keep a single zero if the result is all zeros.
palindrome = palindrome.lstrip('0')
if not palindrome:
return "0"
return palindrome
Case | How to Handle |
---|---|
Input string `num` is null or empty. | Return '0' immediately since no palindrome can be constructed. |
Input string `num` contains only the digit '0'. | If after processing, only '0's remain, return '0' to avoid leading zeros or return the maximal '0' string if it yields a non-empty result. |
Input string `num` contains only a single digit other than zero. | Return the single digit as it is a palindrome. |
Input string `num` contains an odd number of one or more digits. | The middle digit should be the largest single digit with an odd count. |
Input string `num` contains a large number of digits potentially causing memory issues if not handled efficiently. | Use a frequency map to store the counts of digits to avoid storing a large number of digits directly. |
Input `num` has all even counts of digits but contains leading zeros after palindrome construction. | Remove leading zeros from the result, but if the result becomes empty after removing leading zeros, return '0'. |
Input `num` such that only '0's can form the palindrome body. | If the largest palindrome contains only '0' digits, return "0" instead of potentially numerous leading zeros. |
Input contains only '0's in even counts and a '1' in odd count. | Return '1' if no digits are greater than 0 when constructing the prefix. |