Lexicographically n-th permutation of a string
Last Updated :
03 May, 2023
Given a string of length m containing lowercase alphabets only. You have to find the n-th permutation of string lexicographically.
Examples:
Input : str[] = "abc", n = 3
Output : Result = "bac"
Explanation : All possible permutation in
sorted order: abc, acb, bac, bca, cab, cba
Input : str[] = "aba", n = 2
Output : Result = "aba"
Explanation : All possible permutation
in sorted order: aab, aba, baa
Prerequisite : Permutations of a given string using STL Idea behind printing n-th permutation is quite simple we should use STL (explained in above link) for finding next permutation and do it till the nth permutation. After n-th iteration, we should break from the loop and then print the string which is our nth permutation.
long int i = 1;
do
{
// check for nth iteration
if (i == n)
break;
i++; // keep incrementing the iteration
} while (next_permutation(str.begin(), str.end()));
// print string after nth iteration
print str;
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void nPermute(string str, long int n)
{
sort(str.begin(), str.end());
long int i = 1;
do {
if (i == n)
break ;
i++;
} while (next_permutation(str.begin(), str.end()));
cout << str;
}
int main()
{
string str = "GEEKSFORGEEKS" ;
long int n = 100;
nPermute(str, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void nPermute( char [] str, int n)
{
Arrays.sort(str);
int i = 1 ;
do {
if (i == n)
break ;
i++;
} while (next_permutation(str));
System.out.println(String.valueOf(str));
}
static boolean next_permutation( char [] p)
{
for ( int a = p.length - 2 ; a >= 0 ; --a)
if (p[a] < p[a + 1 ])
for ( int b = p.length - 1 ;; --b)
if (p[b] > p[a])
{
char t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1 ; a < b; ++a, --b)
{
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
return false ;
}
public static void main(String[] args)
{
String str = "GEEKSFORGEEKS" ;
int n = 100 ;
nPermute(str.toCharArray(), n);
}
}
|
Python3
def next_permutation(L):
n = len (L)
i = n - 2
while i > = 0 and L[i] > = L[i + 1 ]:
i - = 1
if i = = - 1 :
return False
j = i + 1
while j < n and L[j] > L[i]:
j + = 1
j - = 1
L[i], L[j] = L[j], L[i]
left = i + 1
right = n - 1
while left < right:
L[left], L[right] = L[right], L[left]
left + = 1
right - = 1
return True
def nPermute(string, n):
string = list (string)
new_string = []
string.sort()
j = 2
while next_permutation(string):
new_string = string
if j = = n:
break
j + = 1
print (''.join(new_string))
if __name__ = = "__main__" :
string = "GEEKSFORGEEKS"
n = 100
nPermute(string, n)
|
C#
using System;
class GFG
{
static void nPermute( char [] str, int n)
{
Array.Sort(str);
int i = 1;
do
{
if (i == n)
break ;
i++;
} while (next_permutation(str));
Console.WriteLine(String.Join( "" ,str));
}
static bool next_permutation( char [] p)
{
for ( int a = p.Length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for ( int b = p.Length - 1;; --b)
if (p[b] > p[a])
{
char t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.Length - 1; a < b; ++a, --b)
{
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true ;
}
return false ;
}
public static void Main()
{
String str = "GEEKSFORGEEKS" ;
int n = 100;
nPermute(str.ToCharArray(), n);
}
}
|
Javascript
function nextPermutation(L) {
let n = L.length;
let i = n - 2;
while (i >= 0 && L[i] >= L[i + 1]) {
i--;
}
if (i === -1) {
return false ;
}
let j = i + 1;
while (j < n && L[j] > L[i]) {
j++;
}
j--;
[L[i], L[j]] = [L[j], L[i]];
let left = i + 1;
let right = n - 1;
while (left < right) {
[L[left], L[right]] = [L[right], L[left]];
left++;
right--;
}
return true ;
}
function nPermute(string, n) {
let newString = [];
string = string.split( "" ).sort().join( "" );
let j = 2;
while (nextPermutation(string.split( "" ))) {
newString = string.split( "" );
if (j === n) {
break ;
}
j++;
}
console.log(newString.join( "" ));
}
let string = "GEEKSFORGEEKS" ;
let n = 100;
nPermute(string, n);
|
Time Complexity: O(n + |S| log |S|) Sorting S is log-linear time. `next_permutation` has constant time amortized complexity, however n is independent of |S|, so it is still necessary to include it in the final time complexity notation to properly reflect growth of running time.
Auxiliary Space: O(1) since no extra array is used space occupied by the algorithm is constant
Find n-th lexicographically permutation of a string | Set 2
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...