# Lexicographically largest string possible in one swap

Given a string str of length N, the task is to obtain the lexicographically largest string by at most one swap.
Note: The swapping characters might not be adjacent.

Examples:

Input: str = “string”
Output: tsring
Explanation:
Lexicographically largest string obtained by swapping string -> tsring.

Input: str = “zyxw”
Output: zyxw
Explanation:
The given string is already lexicographically largest.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
To solve the above-mentioned problem, the main idea is to use Sorting and compute the largest lexicographical string possible for the given string. After sorting the given string in descending order, find the first unmatched character from the given string and replace it with the last occurrence of the unmatched character in the sorted string.

Illustration:
str = “geeks”
Sorted string in descending order = “skgee”.
The first unmatched character is in the first place. This character needs to be swapped with the character at this position in the sorted string which results in the lexicographically largest string. On replacing “g” with the “s”, the string obtained is “seekg” which is lexicographically largest after one swap.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to find the ` `// lexicographically largest string ` `// by atmost at most one swap ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to return the ` `// lexicographically largest ` `// string possible by swapping ` `// at most one character ` `string findLargest(string s) ` `{ ` `    ``int` `len = s.size(); ` ` `  `    ``// Stores last occurrence ` `    ``// of every character ` `    ``int` `loccur; ` ` `  `    ``// Initialize with -1 for ` `    ``// every character ` `    ``memset``(loccur, -1, ``sizeof``(loccur)); ` ` `  `    ``for` `(``int` `i = len - 1; i >= 0; --i) { ` ` `  `        ``// Keep updating the last ` `        ``// occurrence of each character ` `        ``int` `chI = s[i] - ``'a'``; ` `        ``// If a previously unvisited ` `        ``// character occurs ` `        ``if` `(loccur[chI] == -1) { ` `            ``loccur[chI] = i; ` `        ``} ` `    ``} ` `    ``// Stores the sorted string ` `    ``string sorted_s = s; ` `    ``sort(sorted_s.begin(), sorted_s.end(), ` `         ``greater<``int``>()); ` ` `  `    ``for` `(``int` `i = 0; i < len; ++i) { ` `        ``if` `(s[i] != sorted_s[i]) { ` ` `  `            ``// Character to replace ` `            ``int` `chI = sorted_s[i] - ``'a'``; ` ` `  `            ``// Find the last occurrence ` `            ``// of this character ` `            ``int` `last_occ = loccur[chI]; ` ` `  `            ``// Swap this with the last ` `            ``// occurrence ` `            ``swap(s[i], s[last_occ]); ` `            ``break``; ` `        ``} ` `    ``} ` ` `  `    ``return` `s; ` `} ` ` `  `// Driver Program ` `int` `main() ` `{ ` `    ``string s = ``"yrstvw"``; ` `    ``cout << findLargest(s); ` `    ``return` `0; ` `} `

Output:

```ywstvr
``` My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.