Skip to main content
Back to problems
Leetcode
Medium
Strings
Greedy
Sorting
Find The Lexicographically Largest String From The Box I

Given a string, choose a lexicographically largest result under the problem’s allowed box-based selection rule.

Acceptance 60%
Problem Statement

Problem

You are given a string s. Think of the string as being placed in a box, and you must build a new string by selecting characters according to the rule described in the problem.

Your task is to return the lexicographically largest string that can be formed from the available characters under that rule.

In lexicographic order, a string is larger if at the first position where two strings differ, it has a larger character.

Goal

Construct the largest possible string according to the allowed operations and return it.

Input Format

  • A single string s.
  • The exact selection rule is implied by the platform problem statement; treat the string as the only input for solving the reconstruction task.

Output Format

  • Return one string: the lexicographically largest string obtainable under the rule.

Constraints

  • 1 <= |s|.
  • The solution should be efficient for typical interview-sized inputs.
  • Characters are compared using standard lexicographic order.
Examples
Sample cases returned by the problem API.

Example 1

Input

s = "bac"

Output

"c"

Explanation

Under a rule that allows selecting from the available box content, the lexicographically largest obtainable single-character result is c.

Example 2

Input

s = "zabc"

Output

"z"

Explanation

The largest character available dominates the result when the rule permits choosing one best character from the box.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.