Watch 6 video solutions for Lexicographically Smallest Permutation Greater Than Target, a medium level problem involving Hash Table, String, Greedy. This walkthrough by Sanyam IIT Guwahati has 2,365 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given two strings s and target, both having length n, consisting of lowercase English letters.
Return the lexicographically smallest permutation of s that is strictly greater than target. If no permutation of s is lexicographically strictly greater than target, return an empty string.
A string a is lexicographically strictly greater than a string b (of the same length) if in the first position where a and b differ, string a has a letter that appears later in the alphabet than the corresponding letter in b.
Example 1:
Input: s = "abc", target = "bba"
Output: "bca"
Explanation:
s (in lexicographical order) are "abc", "acb", "bac", "bca", "cab", and "cba".target is "bca".Example 2:
Input: s = "leet", target = "code"
Output: "eelt"
Explanation:
s (in lexicographical order) are "eelt", "eetl", "elet", "elte", "etel", "etle", "leet", "lete", "ltee", "teel", "tele", and "tlee".target is "eelt".Example 3:
Input: s = "baba", target = "bbaa"
Output: ""
Explanation:
s (in lexicographical order) are "aabb", "abab", "abba", "baab", "baba", and "bbaa".target. Therefore, the answer is "".
Constraints:
1 <= s.length == target.length <= 300s and target consist of only lowercase English letters.Problem Overview: Given a set of characters (or a string whose characters can be rearranged) and a target string, build the lexicographically smallest permutation that is still strictly greater than the target. If multiple permutations exist, return the smallest one in lexicographic order. The difficulty comes from respecting both the remaining character counts and the lexicographic constraint.
Approach 1: Brute Force Enumeration (O(n! * n) time, O(n) space)
Generate every permutation of the characters, compare each permutation with the target, and keep the smallest permutation that is greater than it. Each generated string requires a lexicographic comparison, which takes O(n). This approach works only for very small inputs because permutations grow factorially. It demonstrates the core requirement of the problem but is impractical for interviews or production constraints.
Approach 2: Backtracking with Pruning (O(n! ) worst case, O(n) space)
Use backtracking with a frequency map to generate permutations in lexicographic order. As soon as a permutation becomes smaller than the prefix of the target where it must be larger, prune that branch. This reduces unnecessary exploration compared to full enumeration. A hash table or counting array tracks remaining characters. Although pruning helps in practice, the worst‑case complexity is still factorial.
Approach 3: Greedy Prefix + Counting (O(n * k) time, O(k) space)
The optimal solution builds the answer from left to right. First count the frequency of each character using a hash table. At position i, try placing the smallest available character that keeps the prefix lexicographically valid. If the prefix is still equal to the target, you must try characters greater than or equal to target[i]. When you place a character strictly greater than target[i], the rest of the string can be filled with the smallest remaining characters in sorted order. This greedy construction relies on greedy choice and character string ordering. The alphabet size k is typically small (e.g., 26), giving an efficient O(n * k) solution.
Recommended for interviews: The greedy counting approach is what interviewers expect. It shows you understand lexicographic ordering and how to construct permutations without enumerating all possibilities. Mentioning the brute force or backtracking ideas briefly helps demonstrate the progression from naive enumeration to an efficient greedy construction.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Permutation Enumeration | O(n! * n) | O(n) | Only for very small inputs or conceptual understanding |
| Backtracking with Pruning | O(n!) worst case | O(n) | When exploring permutations but pruning invalid prefixes |
| Greedy Prefix Construction with Counting | O(n * k) | O(k) | General case; optimal for interviews and large inputs |