304 North Cardinal St.
Dorchester Center, MA 02124

# Permutation Sequence LeetCode Solution

## Problem – Permutation Sequence LeetCode Solution

The set `[1, 2, 3, ..., n]` contains a total of `n!` unique permutations.

By listing and labeling all of the permutations in order, we get the following sequence for `n = 3`:

1. `"123"`
2. `"132"`
3. `"213"`
4. `"231"`
5. `"312"`
6. `"321"`

Given `n` and `k`, return the `kth` permutation sequence.

Example 1:

``````Input: n = 3, k = 3
Output: "213"
``````

Example 2:

``````Input: n = 4, k = 9
Output: "2314"
``````

Example 3:

``````Input: n = 3, k = 1
Output: "123"``````

Constraints:

• `1 <= n <= 9`
• `1 <= k <= n!`

## Permutation Sequence LeetCode Solution in Java

``````public class Solution {
public String getPermutation(int n, int k) {
int pos = 0;
List<Integer> numbers = new ArrayList<>();
int[] factorial = new int[n+1];
StringBuilder sb = new StringBuilder();

// create an array of factorial lookup
int sum = 1;
factorial = 1;
for(int i=1; i<=n; i++){
sum *= i;
factorial[i] = sum;
}
// factorial[] = {1, 1, 2, 6, 24, ... n!}

// create a list of numbers to get indices
for(int i=1; i<=n; i++){
}
// numbers = {1, 2, 3, 4}

k--;

for(int i = 1; i <= n; i++){
int index = k/factorial[n-i];
sb.append(String.valueOf(numbers.get(index)));
numbers.remove(index);
k-=index*factorial[n-i];
}

return String.valueOf(sb);
}
``````

## Permutation Sequence LeetCode Solution in C++

``````class Solution {
public:
// Our recursive function that will complete the ans string.
// v - is our current array = [1,2,3,4]
// ans is the answer string, n and k are current values of n and k
// factVal is an array containing the factorial of all integers from 0-9 to get factorial in O(1) time.
// That means I have stored all the factorials in this array before hand to avoid calculation. You can also write factorial funciton if you want.

void setPerm(vector<int>& v,string& ans,int n,int k,vector<int>& factVal){
// if there is only one element left append it to our ans (Base case)
if(n==1){
ans+=to_string(v.back());
return;
}

// We are calculating the required index.  factVal[n-1] means for n =  4 => factVal = 6.
// 15 / 6 = 2 will the index for k =15 and n = 4.
int index = (k/factVal[n-1]);
// if k is a multiple of elements of partition then decrement the index (Corner case I was talking about)
if(k % factVal[n-1] == 0){
index--;
}

ans+= to_string(v[index]);  // add value to string
v.erase(v.begin() + index);  // remove element from array
k -= factVal[n-1] * index;   // adjust value of k; k = 15 - 6*2 = 3.
// Recursive call with n=n-1 as one element is added we need remaing.
setPerm(v,ans,n-1,k,factVal);
}

string getPermutation(int n, int k) {
if(n==1) return "1";
//Factorials of 0-9 stored in the array. factVal = 6. (3! = 6)
vector<int>factVal = {1,1,2,6,24,120,720,5040,40320,362880};
string ans = "";
vector<int> v;
// Fill the array with all elements
for(int i=1;i<=n;i++) v.emplace_back(i);
setPerm(v,ans,n,k,factVal);
return ans;
}
};
``````

## Permutation Sequence LeetCode Solution in Python

``````import math
class Solution:
# @param {integer} n
# @param {integer} k
# @return {string}
def getPermutation(self, n, k):
numbers = range(1, n+1)
permutation = ''
k -= 1
while n > 0:
n -= 1
# get the index of current digit
index, k = divmod(k, math.factorial(n))
permutation += str(numbers[index])
# remove handled number
numbers.remove(numbers[index])

return permutation
``````
##### Permutation Sequence LeetCode Solution Review:

In our experience, we suggest you solve this Permutation Sequence LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Permutation Sequence LeetCode Solution

Find on Leetcode

##### Conclusion:

I hope this Permutation Sequence LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions