The "Lexicographically Smallest Permutation Greater Than Target interview question" is a variation of the "Next Permutation" problem. You are given a source string and a target string. You need to find the lexicographically smallest permutation of the source string that is strictly larger than the target. If no such permutation exists, return -1. This "Lexicographically Smallest Permutation Greater Than Target coding problem" requires a combination of counting and greedy construction.
This problem is asked at companies like Amazon to test a candidate's mastery of the "Greedy interview pattern" and "Counting interview pattern". It is more complex than a standard next permutation because the source and target might be different strings entirely, meaning you must manage a fixed pool of characters (the source) to outpace the target.
The approach is to find the longest common prefix between your construction and the target. At some index i, you must place a character from your remaining pool that is strictly greater than target[i]. To keep the result lexicographically smallest, you pick the smallest character in your pool that is greater than target[i]. After this "diversion," you fill the remaining positions with the remaining characters in ascending order.
Source: "abc", Target: "acb"
When building a string to be "just barely larger" than another, think about the first point of difference. Everything after that point should be as small as possible to keep the overall string as small as possible while still maintaining the "greater than" condition.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Construct K Palindrome Strings | Medium | Solve | |
| Largest Palindromic Number | Medium | Solve | |
| Longest Balanced Substring I | Medium | Solve | |
| Two-Letter Card Game | Medium | Solve | |
| Minimum Number of Keypresses | Medium | Solve |