|
| 1 | +# Largest Palindromic Number |
| 2 | + |
| 3 | +You are given a string num consisting of digits from 0 to 9. Your task is to return the largest possible palindromic |
| 4 | +number as a string by using some or all of the digits in num. |
| 5 | + |
| 6 | +The resulting palindromic number must not have leading zeros. |
| 7 | + |
| 8 | +> Note: You may reorder the digits freely, and you must use at least one digit from the num string. |
| 9 | +
|
| 10 | +## Constraints |
| 11 | + |
| 12 | +- 1 <= `num.length` <= 1000 |
| 13 | +- `num` consists of digits |
| 14 | + |
| 15 | +## Examples |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +## Solution |
| 22 | + |
| 23 | +We will use the greedy pattern to solve this problem. The goal is to maximize the size of the palindrome by making |
| 24 | +locally optimal choices at each step. Specifically, we aim to form the largest possible palindrome by first prioritizing |
| 25 | +using the highest digits. |
| 26 | + |
| 27 | +The process begins by counting the frequency of each digit in the input string and storing it in a hash table. This |
| 28 | +allows us to determine how many times each digit appears. Starting with the highest digit, i.e., 9, and working down to |
| 29 | +the lowest, i.e., 0, we try to use the possible pairs of each digit to form the first half of the palindrome. This |
| 30 | +ensures that the most significant positions in the palindrome are filled with the largest digits. Out of the leftover |
| 31 | +single digits, the highest possible digit can be used as the middle digit to further enhance the size of the palindrome. |
| 32 | + |
| 33 | +Finally, the palindrome is completed by appending the reverse of the first half to itself, with the middle digit in |
| 34 | +between, if applicable. This greedy strategy works effectively because it ensures that each decision made is the best |
| 35 | +possible choice at that moment, leading to an overall optimal solution. |
| 36 | + |
| 37 | +Let’s review the algorithm to reach the solution: |
| 38 | + |
| 39 | +- Initialize the frequency counter occurrences to count the frequency of each digit in the input string num. |
| 40 | +- We also initialize the first_half array to note down the first part of the palindrome and the middle string to track |
| 41 | + the middle element of the palindrome. |
| 42 | +- Traverse digits from 9 to 0 and for each digit do the following: |
| 43 | + - Check if its pairs can be made by looking at its frequency. If yes, then add it to the first_half array. |
| 44 | + - If applicable, check for the leading zeros and avoid them by explicitly setting their occurrence to 1. |
| 45 | + - Otherwise, check if it can be the middle element of the palindrome. Following the greedy approach, ensures that a |
| 46 | + larger number is selected to be the middle element among the elements occurring once. |
| 47 | + |
| 48 | +- Once we have processed all the elements of the num array, we join the first_half, middle, and reverse of the first_half. |
| 49 | +- Finally, we return this palindromic number, the largest that can be generated using the given number. |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +### Time Complexity |
| 62 | + |
| 63 | +The time complexity of the solution is O(n), where n is the length of the num string. |
| 64 | + |
| 65 | +### Space Complexity |
| 66 | + |
| 67 | +The space complexity of the solution is O(n). In the worst case, first_half could store up to n/2 digits if all digits |
| 68 | +are the same. Therefore, the space for first_half is O(n). |
0 commit comments