Problem solution · Python

Smallest Palindromic Rearrangement II

Smallest Palindromic Rearrangement II: a Python solution using hash-based lookup. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Hash-based lookup
Source
walkccc LeetCode Solutions
Length
70 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Hash-based lookup

For Smallest Palindromic Rearrangement II, the implementation stores previously seen values or frequencies in a hash table for direct membership and lookup operations.

  1. Decide the key that represents the information needed later.
  2. Update its count or stored state while scanning the input.
  3. Use constant-time expected lookups to detect matches or assemble the result.

Code notes

  • 70 lines of Python from the credited upstream file 3518.py.
  • The implementation visibly relies on sequence storage, hash lookup.
  • No explicit loop blocks detected.

Complexity

Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeSmallest Palindromic Rearrangement II · PythonPython
Use this to learn the idea, then write your own version.
class Solution:  def __init__(self):    self.MAX = 10**6 + 1   def smallestPalindrome(self, s: str, k: int) -> str:    count = collections.Counter(s)    if not self._isPalindromePossible(count):      return ''     halfCount, midLetter = self._getHalfCountAndMidLetter(count)    totalPerm = self._calculateTotalPermutations(halfCount)    if k > totalPerm:      return ''    leftHalf = self._generateLeftHalf(halfCount, k)    return ''.join(leftHalf) + midLetter + ''.join(reversed(leftHalf))   def _isPalindromePossible(self, count: collections.Counter) -> bool:    oddCount = sum(1 for count in count.values() if count % 2 == 1)    return oddCount <= 1   def _getHalfCountAndMidLetter(self, count: collections.Counter) -> tuple[list[int], str]:    halfCount = [0] * 26    midLetter = ''    for c, freq in count.items():      halfCount[ord(c) - ord('a')] = freq // 2      if freq % 2 == 1:        midLetter = c    return halfCount, midLetter   def _calculateTotalPermutations(self, halfCount: list[int]) -> int:    """Calculate the total number of possible permutations."""    return self._countArrangements(halfCount)   def _generateLeftHalf(self, halfCount: list[int], k: int) -> list[str]:    """Generate the left half of the palindrome based on k."""    halfLen = sum(halfCount)    left = []    for _ in range(halfLen):      for i, freq in enumerate(halfCount):        if freq == 0:          continue        halfCount[i] -= 1        arrangements = self._countArrangements(halfCount)        if arrangements >= k:          left.append(chr(i + ord('a')))          break        else:          k -= arrangements          halfCount[i] += 1    return left   def _countArrangements(self, count: list[int]) -> int:    """Calculate the number of possible arrangements of characters."""    total = sum(count)    res = 1    for freq in count:      res *= self._nCk(total, freq)      if res >= self.MAX:        return self.MAX      total -= freq    return res   def _nCk(self, n: int, k: int) -> int:    res = 1    for i in range(1, min(k, n - k) + 1):      res = res * (n - i + 1) // i      if res >= self.MAX:        return self.MAX    return res 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗