Advertisement

Largest Palindromic Number - LeetCode 2384 Solution

Largest Palindromic Number - Complete Solution Guide

Largest Palindromic Number is LeetCode problem 2384, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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: Input: num = "444947137" Output: "7449447" Explanation: Use the digits "4449477" from " 44494 7 13 7 " to form the palindromic integer "7449447". It can be shown

Detailed Explanation

The problem requires us to construct the largest possible palindromic number using digits from a given string 'num'. We can use each digit in 'num' as many times as it appears, but we don't have to use all the digits. The resulting palindromic number should not have any leading zeros, unless the entire number is zero.

Solution Approach

The solution first counts the frequency of each digit in the input string 'num'. Then, it greedily builds the left half of the palindrome using pairs of digits from 9 down to 0. Special care is taken to handle '0' so leading zeros can be avoided. The largest single digit appearing an odd number of times is selected as the middle digit. Finally, the right half of the palindrome is constructed by reversing the left half and concatenating the left half, middle digit (if any), and reversed left half to get the final result.

Step-by-Step Algorithm

  1. Step 1: Count the occurrences of each digit in the input string `num` using a hash map or an array.
  2. Step 2: Create an empty string or list to store the left half of the palindrome.
  3. Step 3: Iterate through the digits from '9' to '1'. For each digit, calculate the number of pairs (frequency // 2) and append the digit to the left half string as many times as the number of pairs.
  4. Step 4: If the left half is not empty, add pairs of '0' to the left half. This is important for handling cases like '00009', ensuring that we don't start with zeros unless we have other digits.
  5. Step 5: Find the largest digit that appears an odd number of times. This digit will be the middle digit of the palindrome.
  6. Step 6: Create the full palindrome by concatenating the left half, the middle digit (if any), and the reversed left half.
  7. Step 7: Handle edge cases: if the result is empty return '0'. Remove leading zeros while also making sure not to cut off the palindrome to single zero.
  8. Step 8: Return the final palindromic string.

Key Insights

  • Insight 1: A palindrome is a number that reads the same forwards and backward. Therefore, we should aim to build the left half of the palindrome and then mirror it to create the right half.
  • Insight 2: To maximize the palindromic number, we should prioritize using larger digits for the outer parts of the palindrome. Therefore we process digits from '9' down to '0'.
  • Insight 3: We can only have one digit appearing an odd number of times in the middle of the palindrome. We choose the largest one that appears an odd number of times to maximize our palindrome.
  • Insight 4: Zeros can be tricky. We can only add pairs of zeros to the middle section if we have already added other digits, otherwise the palindrome must be either a single zero or non-existent.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Hash Table, String, Greedy, Counting.

Companies

Asked at: Bentley Systems, Geico, smartnews.