3720. Lexicographically Smallest Permutation Greater Than Target Medium

@problem@discussion
#Hash Table#String#Greedy#Counting#Enumeration



1/**
2 * [3720] Lexicographically Smallest Permutation Greater Than Target
3 *
4 * You are given two strings s and target, both having length n, consisting of lowercase English letters.
5 * Return the lexicographically smallest <span data-keyword="permutation-string">permutation</span> of s that is strictly greater than target. If no permutation of s is lexicographically strictly greater than target, return an empty string.
6 * 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.
7 *  
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">s = "abc", target = "bba"</span>
11 * Output: <span class="example-io">"bca"</span>
12 * Explanation:
13 * 
14 * 	The permutations of s (in lexicographical order) are "abc", "acb", "bac", "bca", "cab", and "cba".
15 * 	The lexicographically smallest permutation that is strictly greater than target is "bca".
16 * </div>
17 * <strong class="example">Example 2:
18 * <div class="example-block">
19 * Input: <span class="example-io">s = "leet", target = "code"</span>
20 * Output: <span class="example-io">"eelt"</span>
21 * Explanation:
22 * 
23 * 	The permutations of s (in lexicographical order) are "eelt", "eetl", "elet", "elte", "etel", "etle", "leet", "lete", "ltee", "teel", "tele", and "tlee".
24 * 	The lexicographically smallest permutation that is strictly greater than target is "eelt".
25 * </div>
26 * <strong class="example">Example 3:
27 * <div class="example-block">
28 * Input: <span class="example-io">s = "baba", target = "bbaa"</span>
29 * Output: <span class="example-io">""</span>
30 * Explanation:
31 * 
32 * 	The permutations of s (in lexicographical order) are "aabb", "abab", "abba", "baab", "baba", and "bbaa".
33 * 	None of them is lexicographically strictly greater than target. Therefore, the answer is "".
34 * </div>
35 *  
36 * Constraints:
37 * 
38 * 	1 <= s.length == target.length <= 300
39 * 	s and target consist of only lowercase English letters.
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/lexicographically-smallest-permutation-greater-than-target/
45// discuss: https://leetcode.com/problems/lexicographically-smallest-permutation-greater-than-target/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50    pub fn lex_greater_permutation(s: String, target: String) -> String {
51        String::new()
52    }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_3720() {
63    }
64}
65

Back
© 2026 bowen.ge All Rights Reserved.