Skip to main content

Lexicographically Smallest Palindromic Permutation Greater Than Target - Solution & Explanation

Practice this problem

Problem Statement

You are given two strings s and target, each of length n, consisting of lowercase English letters.

Return the lexicographically smallest string that is both a palindromic permutation of s and strictly greater than target. If no such permutation exists, return an empty string.

 

Example 1:

Input: s = "baba", target = "abba"

Output: "baab"

Explanation:

  • The palindromic permutations of s (in lexicographical order) are "abba" and "baab".
  • The lexicographically smallest permutation that is strictly greater than target is "baab".

Example 2:

Input: s = "baba", target = "bbaa"

Output: ""

Explanation:

  • The palindromic permutations of s (in lexicographical order) are "abba" and "baab".
  • None of them is lexicographically strictly greater than target. Therefore, the answer is "".

Example 3:

Input: s = "abc", target = "abb"

Output: ""

Explanation:

s has no palindromic permutations. Therefore, the answer is "".

Example 4:

Input: s = "aac", target = "abb"

Output: "aca"

Explanation:

  • The only palindromic permutation of s is "aca".
  • "aca" is strictly greater than target. Therefore, the answer is "aca".

 

Constraints:

  • 1 <= n == s.length == target.length <= 300
  • s and target consist of only lowercase English letters.

Approach Overview

Problem Overview: Given a string, return the lexicographically smallest palindromic permutation that is strictly greater than the target string. If no such palindrome exists, return an empty string. The key constraint is that the resulting string must both be a permutation of the characters and remain a palindrome.

Approach 1: Brute Force Enumeration (Factorial Time, O(n!) time, O(n) space)

The direct strategy is to generate every permutation of the string, filter the ones that form palindromes, and keep the smallest palindrome that is lexicographically greater than the target. You check each permutation by comparing characters from both ends using two pointers. While easy to reason about, the permutation space grows as n!, which becomes infeasible even for moderate string sizes. This approach mainly demonstrates the constraints of the problem rather than serving as a practical solution.

Approach 2: Half String Permutation + Next Lexicographic Order (Optimal) (O(n log n) time, O(n) space)

A palindrome is fully determined by its first half. Count the frequency of each character in the string. Build a sorted half-string using freq[c] // 2 copies of each character and keep a middle character if any count is odd. Construct the smallest palindrome by mirroring this half. If that palindrome is already greater than the target, return it.

If not, compute the next lexicographic permutation of the half-string using the classic next_permutation technique: scan from the right to find the first decreasing position, swap with the next larger character, then reverse the suffix. Each time a new half is produced, mirror it to rebuild the palindrome and compare with the target. This works because lexicographic ordering of palindromes corresponds directly to ordering of their first halves. The process effectively enumerates valid palindromes in sorted order without exploring the entire permutation space.

Approach 3: Enumeration with Early Pruning (O(n · k) time, O(n) space)

Another view treats the half-string generation as controlled enumeration. Build candidate halves character by character while maintaining lexicographic ordering. If a prefix already exceeds the corresponding prefix of the target's half, the remaining positions can be filled with the smallest characters. This prunes large parts of the search space compared to brute force. It is conceptually useful but typically more complex than simply applying next_permutation.

Recommended for interviews: The half-string permutation with next_permutation is the approach interviewers expect. It leverages the structural property of palindromes and reduces the search space from n! permutations to permutations of only half the string. Showing the brute force idea demonstrates understanding of the problem space, while the optimized method shows algorithmic maturity and familiarity with lexicographic permutation techniques.

Solution

Code

Rust

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force PermutationO(n!)O(n)Conceptual baseline or very small strings
Half String + Next PermutationO(n log n)O(n)General case and expected interview solution
Pruned Enumeration of HalfO(n · k)O(n)When exploring ordered candidates with prefix pruning

Video Solution

Lexicographically Smallest Palindromic Permutation Greater Than Target | LeetCode 3734Sanyam IIT Guwahati818 views views

Watch 2 more video solutions →

Frequently Asked Questions

Is Lexicographically Smallest Palindromic Permutation Greater Than Target easy or hard?
This problem is considered Hard because it combines multiple ideas: palindrome construction, lexicographic ordering, permutation generation, and careful string comparison. While each concept individually is common, integrating them efficiently without exploring all permutations requires deeper algorithmic insight.
Lexicographically Smallest Palindromic Permutation Greater Than Target Python/Java solution
Implementations in Python, Java, C++, and Go follow the same steps: count character frequencies, build a sorted half-string, apply next_permutation to that half, and mirror it around an optional middle character. After each permutation, compare the generated palindrome with the target. The first valid palindrome greater than the target is returned.
How to solve Lexicographically Smallest Palindromic Permutation Greater Than Target in O(n)?
Near-linear performance comes from avoiding full permutation generation. Count characters, build the smallest half-string, and mirror it to form the minimal palindrome. If it is not greater than the target, repeatedly apply next_permutation on the half until a larger palindrome appears. Each step only rearranges half the string, keeping operations close to O(n).
What is the best approach for Lexicographically Smallest Palindromic Permutation Greater Than Target?
The most effective approach constructs the palindrome from its half-string representation and applies the next lexicographic permutation on that half. Because a palindrome is determined by its first half and optional middle character, generating permutations of only the half drastically reduces the search space. Each candidate half is mirrored to form a full palindrome and compared with the target. This runs around O(n log n) time with O(n) space.
Is Lexicographically Smallest Palindromic Permutation Greater Than Target asked at Google/Amazon/Meta?
Problems involving lexicographic ordering, palindromes, and permutation generation frequently appear in interviews at large tech companies such as Google, Amazon, and Meta. Variants often test understanding of next_permutation, string manipulation, and combinatorial reasoning. This problem combines those concepts in a harder format.
What data structure is used in Lexicographically Smallest Palindromic Permutation Greater Than Target?
The core structures are a frequency array or hash map for counting characters and a character array representing the half of the palindrome. Two-pointer logic is used when verifying palindrome properties or reconstructing the mirrored string. Lexicographic permutation logic operates directly on the half-string array.
What is the time complexity of Lexicographically Smallest Palindromic Permutation Greater Than Target?
The optimal solution runs in roughly O(n log n) time. Building the frequency map and initial half-string takes O(n), and each next_permutation operation on the half-string costs O(n). Space complexity is O(n) for storing the half and reconstructed palindrome.

Ready to solve this problem?

Practice Lexicographically Smallest Palindromic Permutation Greater Than Target with our built-in code editor and test cases.

Practice on FleetCode