Largest Palindromic Number - Counting & Greedy [JS]
- Get link
- X
- Other Apps
By
Anna An
on
Solution: Counting & Greedy
- Count the number of occurances of each digit in num.
- Construct the first half of the number.
- It is always optimal to put larger digits earlier on.
- Going from 9 to 0, add digits to the end of result while the count of the digit > 1.
- We use two occurances per new digit since it needs to be symmetrical.
- Get the largest possible single digit as the middle of the palindrome.
- Add the second half of the palindrome. This is just the symmetrical right side of the left half.
Time Complexity: O(n)
135ms
Space Complexity: O(n)
54.2MB
var largestPalindromic = function(num) {
let count = Array(10).fill(0);
for (let char of num) {
let digit = Number(char);
count[digit]++;
}
let res = [];
for (let i = 9; i >= 0; i--) {
if (i === 0 && res.length === 0) break; // no leading zeros
while (count[i] > 1) {
res.push(i.toString());
count[i] -= 2;
}
}
let hasSingleMid = false;
for (let i = 9; i >= 0; i--) {
if (count[i] > 0) {
res.push(i.toString());
hasSingleMid = true;
break;
}
}
for (let i = res.length - (hasSingleMid ? 2 : 1); i >= 0; i--) {
res.push(res[i]);
}
return res.join("");
};
javascript
- Get link
- X
- Other Apps
Comments
Post a Comment