Lexicographically Smallest Permutation Greater Than Target Problem
Description
LeetCode Problem 3720.
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:
- The permutations of s (in lexicographical order) are "abc", "acb", "bac", "bca", "cab", and "cba".
- The lexicographically smallest permutation that is strictly greater than target is "bca".
Example 2:
Input: s = "leet", target = "code"
Output: "eelt"
Explanation:
- The permutations of s (in lexicographical order) are "eelt", "eetl", "elet", "elte", "etel", "etle", "leet", "lete", "ltee", "teel", "tele", and "tlee".
- The lexicographically smallest permutation that is strictly greater than target is "eelt".
Example 3:
Input: s = "baba", target = "bbaa"
Output: ""
Explanation:
- The permutations of s (in lexicographical order) are "aabb", "abab", "abba", "baab", "baba", and "bbaa".
- None of them is lexicographically strictly greater than target. Therefore, the answer is "".
Constraints:
1 <= s.length == target.length <= 300sandtargetconsist of only lowercase English letters.
Solution
Approach: Greedy + Backtracking
A permutation strictly greater than target shares some prefix with it, then has a larger letter at the next position, then the leftover letters in ascending order. The longer the shared prefix, the smaller the result, so look for the longest prefix first.
Count the letters of s. Walk along target, taking each letter while it is still available, and stop when one runs out; that is the longest prefix we can copy. Then step back from the end of this prefix: at position i, try to place the smallest available letter greater than target[i]. If there is one, append the remaining letters in ascending order and return. Otherwise return target[i - 1] to the count and try the position before it. If target is itself a permutation of s, position n offers nothing greater, so the search starts at the last position. If every position fails, return the empty string.
Complexity: O(26 n) time and O(n + 26) space.
from collections import Counter
from string import ascii_lowercase
class Solution:
def lexGreaterPermutation(self, s: str, target: str) -> str:
cnt = Counter(s)
n = len(target)
ans = []
for c in target:
if cnt[c] == 0:
break
cnt[c] -= 1
ans.append(c)
for i in range(len(ans), -1, -1):
if i < n:
for c in ascii_lowercase:
if c > target[i] and cnt[c] > 0:
cnt[c] -= 1
rest = ''.join(x * cnt[x] for x in ascii_lowercase)
return ''.join(ans[:i]) + c + rest
if i > 0:
cnt[ans[i - 1]] += 1
return ''