#3734

Lexicographically Smallest Palindromic Permutation Greater Than Target

Hard
Two PointersStringEnumeration
LeetCode ↗

Approaches

💡

Intuition

Time Space

Generate all permutations of the string and filter for palindromic ones. Then check which is greater than the target.

⚙️

Algorithm

3 steps
  1. 1Step 1: Generate all unique permutations of the string.
  2. 2Step 2: Filter the permutations to find palindromic ones.
  3. 3Step 3: Sort the palindromic permutations and find the smallest one greater than the target.
solution.py9 lines
1from itertools import permutations
2
3def smallest_palindrome_permutation(s, target):
4    perms = set([''.join(p) for p in permutations(s)])
5    palindromes = sorted(p for p in perms if p == p[::-1])
6    for p in palindromes:
7        if p > target:
8            return p
9    return ''

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.