Largest Palindromic Number LeetCode Solution

Problem – Largest Palindromic Number

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 "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.

Largest Palindromic Number LeetCode Solution in Python

    def largestPalindromic(self, num: str) -> str:
        count = Counter(num)
        res = ''.join(count[i] // 2 * i for i in '9876543210').lstrip('0')
        mid = max(count[i] % 2 * i for i in count)
        return (res + mid + res[::-1]) or '0'

Largest Palindromic Number LeetCode Solution in C++

class Solution {
public:
    string largestPalindromic(string num) {
        vector<int> cnt(10);
        for (char c: num) {
            cnt[c - '0']++;
        }
        
        string lp, rp; // left and right partition
        for (int i: num) {
            for (int j = 9; j >= 0; j--)  {
                if (cnt[j] > 1 && (j > 0 || lp.size())) {
                    lp += '0' + j;
                    rp += '0' + j;
                    cnt[j] -= 2;
                    break;
                }
            }
        }
        for (int i = 9; i >= 0; i--) {
            if (cnt[i]) {
                lp += '0' + i; break;
            }
        }
        reverse(begin(rp), end(rp));
        return lp + rp;
    }
};

Largest Palindromic Number LeetCode Solution in Java

class Solution {
    public String largestPalindromic(String num) {
        int[] n = new int[10];
        for (char c : num.toCharArray()) {
            n[c - '0']++;
        }
        boolean only = false;
		//seperate the result into 3 parts, the first half and second half, also the center (if exist)
        StringBuilder sbPre = new StringBuilder();
        StringBuilder sbPost = new StringBuilder();
        StringBuilder sbOnly = new StringBuilder();
		//check from 9 to 1, to make it the largest
        for (int i = 9; i >= 0; i--) {
		    //check for center
            if (n[i] % 2 != 0 && !only) {
                only = true;
                sbOnly.append(Integer.toString(i));
            }
			//handle special case of all '0' or '0' is the first digit(invalid) 
            if (i == 0 && sbPre.length() == 0) {
                if (n[i] > 0 && sbOnly.length() == 0) sbOnly.append(Integer.toString(i));
                return sbOnly.toString();
            }
            for (int j = 0; j < n[i] / 2; j++) {
                sbPre.append(Integer.toString(i));
                sbPost.insert(0, Integer.toString(i));
            }
        }
        sbPre.append(sbOnly);
        sbPre.append(sbPost);
        return sbPre.toString();
    }
}
Largest Palindromic Number LeetCode Solution Review:

In our experience, we suggest you solve this Largest Palindromic Number LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Largest Palindromic Number LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Largest Palindromic Number LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.