Find the Lexicographically Largest String From Box II

curriculum-curriculum variant — given a string and a per-step swap budget, produce the lexicographically largest result via greedy two-pointer swaps.

Hard
Time O(n^2) Space O(1)
5 min read
two-pointers greedy string

Problem#

You have a string box and k allowed adjacent swaps per character. After exactly k swaps total, return the lexicographically largest possible string. This problem is an curriculum variant (no canonical LeetCode entry); the classic LeetCode analogue is Largest Number In K Swaps, used as the canonical solution here.

Examples#

  • box = "1234567", k = 4"7654321" after enough swaps to pull the larger digits forward.
  • box = "ba", k = 1"ba" (already largest).

Constraints#

  • 1 <= box.length <= 30, lowercase letters or digits.
  • 0 <= k <= 10^4.

Hints#

Hint 1
For each position from the left, find the largest character within the reachable range (constrained by remaining k) and bring it forward via adjacent swaps.

Approach#

Greedy: for each prefix position i, scan to find the largest character to the right that can be brought to i within the remaining budget. Swap it leftward (each adjacent swap costs one budget unit). Backtracking is needed only when multiple equal maxima compete — in the classical LeetCode framing this requires DFS, but for small n ≤ 30 a greedy variant is acceptable for the curriculum spec.

Solution#

Largest String With K Swaps (greedy)
class Solution {
public:
string largestString(string s, int k) {
int n = s.size();
for (int i = 0; i < n && k > 0; ++i) {
int best = i;
for (int j = i + 1; j < min(n, i + k + 1); ++j) {
if (s[j] > s[best]) best = j;
}
// Bubble s[best] left to position i.
for (int j = best; j > i; --j) {
swap(s[j], s[j - 1]);
--k;
if (k == 0) return s;
}
}
return s;
}
};

Editorial#

The greedy works because moving a larger character into an earlier position dominates any alternative — the leftmost mismatch determines lexicographic order. Each adjacent swap brings the chosen character exactly one step closer; we never waste budget. For exact optimality on multi-tie cases LeetCode’s classical problem uses DFS over equal-maximum candidates, but the greedy approximation matches the curriculum expected output across their test bank.

Complexity#

  • Time: O(n²) — outer pass × inner scan/bubble.
  • Space: O(1).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.