Skip to main content
  1. LeetCode/

LeetCode 3517: Smallest Palindromic Rearrangement I

·1 min· ·
LeetCode Weekly Medium
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents
LeetCode Weekly Contest 445 - This article is part of a series.
Part 2: This Article

Meta Data
#

Difficulty: medium First Attempt: 2025-04-13

  • Total time: 05:00.00

Intuition
#

We need to determine whether the string has an odd or even length in order to check if there’s a middle character that needs special handling.
Then, we sort the first half of the string based on ord(), and construct the final result by concatenating the left half, the middle character (if any), and the reversed left half.

Approach
#

class Solution:
    def smallestPalindrome(self, s: str) -> str:
        odd = len(s)%2
        helf_index = len(s)//2
        left = list(s[:helf_index])
        
        middle_str = s[helf_index] if odd else ""
        
        left_sorted = "".join(sorted(left, key=ord))
        
        return left_sorted+middle_str+left_sorted[::-1]

Findings
#

NA

Encountered Problems
#

NA

LeetCode Weekly Contest 445 - This article is part of a series.
Part 2: This Article

Related

LeetCode 3516: Find Closest Person
·1 min
LeetCode Weekly Easy
LeetCode Problem Solving
LeetCode 1922: Count Good Numbers
·2 mins
LeetCode Daily Medium
LeetCode Problem Solving
LeetCode 556: Next Greater Element III
·1 min
LeetCode Medium Next Permutation
LeetCode Problem Solving
LeetCode 3513: Number of Unique XOR Triplets I
·2 mins
LeetCode Bi-Weekly Medium Unsolved Bit
LeetCode Problem Solving
LeetCode 3512: Minimum Operations to Make Array Sum Divisible by K
·1 min
LeetCode Bi-Weekly Easy
LeetCode Problem Solving
LeetCode 2843: Count Symmetric Integers
·1 min
LeetCode Daily Easy Digit DP
LeetCode Problem Solving