10. Largest Number in One Swap
β GFG solution to the Largest Number in One Swap problem: find lexicographically largest string by swapping at most one pair of characters using greedy approach. π
π§© Problem Description
π Examples
Example 1
Input: s = "768"
Output: "867"
Explanation: Swapping the 1st and 3rd characters (7 and 8 respectively),
gives the lexicographically largest string.Example 2
Input: s = "333"
Output: "333"
Explanation: Performing any swaps gives the same result i.e "333".π Constraints
β
My Approach
Greedy Single Pass Algorithm
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated