Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Contributors.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
12. [Hacker-set](https://github.com/Hacker-set)]
13. [Prateek Sengar]https://github.com/prtksengar3)
14. [ANant Rungta](https://github.com/Anant016)

15. [Pratyay Banerjee](https://github.com/Neilblaze)
54 changes: 54 additions & 0 deletions Next_permutation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Implement next permutation algorithm. i.e
* Given a word w, rearrange the letters of w to construct another word s in such a way that
* s is lexicographically greater than w.
* In case of multiple possible answers, find the lexicographically smallest one.
* Example:
* ab --> ba
* bb --> bb
* hefg --> hegf
* dhck --> dhkc
* dkhc --> hcdk
*/

#include <iostream>

std::string
next_permutation( std::string str)
{
int len = str.length();
int i = len -1;
// We will iterate string from end, and once we have encountered a pair of letters
// such that str[i] < str[i-1] , i-1 would become our pivot
while( i > 0 && str[i] <= str[i-1] ) {
--i;
}
if ( i == 0 ) {
return str;
}
//our pivot right now would be str[i-1]
int j = len - 1;
while( j >= i && str[j] <= str[i-1] ) {
--j;
}
std::swap(str[i-1], str[j]);
j = len - 1;
//reverse the suffix
while( i < j ) {
std::swap(str[i], str[j]);
--j;
++i;
}
return str;
}

int main()
{
std::string str, str_next;
std::cout << "Enter a string : ";
std::cin >> str;
str_next = next_permutation(str);
std::cout << "Next permutation of " << str << " is "
<< str_next << std::endl;
return 0;
}