3720. Lexicographically Smallest Permutation Greater Than Target
</> Solution
/**
* Problem: LeetCode 3720 - Lexicographically Smallest Permutation Greater Than Target
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public String lexGreaterPermutation(String s, String target) {
int n = s.length();
int[] cnt = new int[26];
for (char c : s.toCharArray()) {
cnt[c - 'a']++;
}
int breakIndex = -1;
int[] breakCnt = null;
for (int i = 0; i < n; i++) {
int tc = target.charAt(i) - 'a';
// check if a character greater than target[i] is available right now
boolean found = false;
for (int c = tc + 1; c < 26; c++) {
if (cnt[c] > 0) {
found = true;
break;
}
}
if (found) {
breakIndex = i; // candidate breakpoint (keep the rightmost one)
breakCnt = cnt.clone(); // save state at this point
}
// try to continue matching target as prefix
if (cnt[tc] > 0) {
cnt[tc]--;
} else {
break; // can't match prefix any further, no later breakpoints possible
}
}
if (breakIndex == -1) {
return ""; // no valid permutation strictly greater than target
}
StringBuilder sb = new StringBuilder();
sb.append(target, 0, breakIndex); // matched prefix
int tc = target.charAt(breakIndex) - 'a';
int chosen = -1;
for (int c = tc + 1; c < 26; c++) {
if (breakCnt[c] > 0) {
chosen = c;
break;
}
}
breakCnt[chosen]--;
sb.append((char) ('a' + chosen)); // smallest char strictly greater than target[breakIndex]
// fill the rest with remaining letters in ascending order (smallest arrangement)
for (int c = 0; c < 26; c++) {
for (int k = 0; k < breakCnt[c]; k++) {
sb.append((char) ('a' + c));
}
}
return sb.toString();
}
}
"""
Problem: LeetCode 3720 - Lexicographically Smallest Permutation Greater Than Target
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
class Solution:
def lexGreaterPermutation(self, s: str, target: str) -> str:
n = len(s)
cnt = [0] * 26
for ch in s:
cnt[ord(ch) - ord("a")] += 1
break_index = -1
break_cnt = None
for i in range(n):
tc = ord(target[i]) - ord("a")
# check if a character greater than target[i] is available right now
if any(cnt[c] > 0 for c in range(tc + 1, 26)):
break_index = i
break_cnt = list(cnt)
# try to continue matching target as prefix
if cnt[tc] > 0:
cnt[tc] -= 1
else:
break
if break_index == -1:
return ""
res = [target[:break_index]]
tc = ord(target[break_index]) - ord("a")
chosen = -1
for c in range(tc + 1, 26):
if break_cnt[c] > 0:
chosen = c
break
break_cnt[chosen] -= 1
res.append(chr(ord("a") + chosen))
for c in range(26):
if break_cnt[c] > 0:
res.append(chr(ord("a") + c) * break_cnt[c])
return "".join(res)
/**
* Problem: LeetCode 3720 - Lexicographically Smallest Permutation Greater Than Target
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <string>
#include <vector>
using namespace std;
/**
* Problem: LeetCode 3720 - Lexicographically Smallest Permutation Greater Than Target
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
string lexGreaterPermutation(string s, string target) {
int n = s.length();
vector<int> cnt(26, 0);
for (char c : s) {
cnt[c - 'a']++;
}
int breakIndex = -1;
vector<int> breakCnt;
for (int i = 0; i < n; i++) {
int tc = target[i] - 'a';
// check if a character greater than target[i] is available right now
bool found = false;
for (int c = tc + 1; c < 26; c++) {
if (cnt[c] > 0) {
found = true;
break;
}
}
if (found) {
breakIndex = i; // candidate breakpoint (keep the rightmost one)
breakCnt = cnt; // save state at this point
}
// try to continue matching target as prefix
if (cnt[tc] > 0) {
cnt[tc]--;
} else {
break; // can't match prefix any further, no later breakpoints possible
}
}
if (breakIndex == -1) {
return ""; // no valid permutation strictly greater than target
}
string result;
result.reserve(n);
result.append(target, 0, breakIndex); // matched prefix
int tc = target[breakIndex] - 'a';
int chosen = -1;
for (int c = tc + 1; c < 26; c++) {
if (breakCnt[c] > 0) {
chosen = c;
break;
}
}
breakCnt[chosen]--;
result.push_back((char)('a' + chosen)); // smallest char strictly greater than target[breakIndex]
// fill the rest with remaining letters in ascending order (smallest arrangement)
for (int c = 0; c < 26; c++) {
if (breakCnt[c] > 0) {
result.append(breakCnt[c], (char)('a' + c));
}
}
return result;
}
};
/**
* Problem: LeetCode 3720 - Lexicographically Smallest Permutation Greater Than Target
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var lexGreaterPermutation = function(s, target) {
const n = s.length;
const cnt = new Array(26).fill(0);
const codeA = 97; // 'a'.charCodeAt(0)
for (let i = 0; i < n; i++) {
cnt[s.charCodeAt(i) - codeA]++;
}
let breakIndex = -1;
let breakCnt = null;
for (let i = 0; i < n; i++) {
const tc = target.charCodeAt(i) - codeA;
// check if a character greater than target[i] is available right now
let found = false;
for (let c = tc + 1; c < 26; c++) {
if (cnt[c] > 0) {
found = true;
break;
}
}
if (found) {
breakIndex = i; // candidate breakpoint (keep the rightmost one)
breakCnt = cnt.slice(); // save state at this point
}
// try to continue matching target as prefix
if (cnt[tc] > 0) {
cnt[tc]--;
} else {
break; // can't match prefix any further, no later breakpoints possible
}
}
if (breakIndex === -1) {
return ""; // no valid permutation strictly greater than target
}
let res = target.substring(0, breakIndex); // matched prefix
const tc = target.charCodeAt(breakIndex) - codeA;
let chosen = -1;
for (let c = tc + 1; c < 26; c++) {
if (breakCnt[c] > 0) {
chosen = c;
break;
}
}
breakCnt[chosen]--;
res += String.fromCharCode(codeA + chosen); // smallest char strictly greater than target[breakIndex]
// fill the rest with remaining letters in ascending order (smallest arrangement)
for (let c = 0; c < 26; c++) {
if (breakCnt[c] > 0) {
res += String.fromCharCode(codeA + c).repeat(breakCnt[c]);
}
}
return res;
};