bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 2384. Largest Palindromic Number

2384. Largest Palindromic Number

Leetcode Solutions

Counting and Greedy Approach

  1. Initialize an array count of size 10 to store the frequency of each digit (0-9) in the input string num.
  2. Iterate over the input string num and increment the corresponding index in the count array.
  3. Initialize two strings leftPart and rightPart to build the left and right halves of the palindrome.
  4. Iterate over the digits from 9 to 0, and for each digit that has a frequency greater than 1, append it to leftPart and rightPart while decrementing its count by 2.
  5. Find the largest digit that has an odd frequency and place it in the middle of the palindrome if such a digit exists.
  6. Reverse rightPart and concatenate leftPart, the middle digit (if any), and rightPart to form the final palindrome.
  7. Return the final palindrome string.
UML Thumbnail

Brute Force with Optimization

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...