484. Find Permutation
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to reconstruct the lexicographically smallest permutation of integers from 1 to n based on a descriptive string s of length n-1. The string s consists of characters 'I' and 'D', where 'I' denotes an increasing relationship (perm[i] perm[i+1]).
Solution
Find Permutation in Python
class Solution:
def findPermutation(self, s: str) -> List[int]:
n = len(s) + 1
# Initialize perm with [1, 2, 3, ..., n]
perm = list(range(1, n + 1))
i = 0
while i < len(s):
# If we see an 'I', the current order is fine, move on.
if s[i] == 'I':
i += 1
continue
# If we see a 'D', it's the start of a decreasing sequence.
# Find the end of this consecutive 'D' block.
j = i
while j + 1 < len(s) and s[j + 1] == 'D':
j += 1
# The 'D' block from s[i...j] corresponds to perm[i...j+1].
# We need to reverse this segment of the permutation.
# For example, s="D", i=0, j=0, reverse perm[0...1].
# For s="DD", i=0, j=1, reverse perm[0...2].
left, right = i, j + 1
while left < right:
perm[left], perm[right] = perm[right], perm[left]
left += 1
right -= 1
# Move past the processed 'D' block.
i = j + 1
return perm
Find Permutation in Java
class Solution {
public int[] findPermutation(String s) {
int n = s.length() + 1;
int[] perm = new int[n];
// Initialize perm with [1, 2, 3, ..., n]
for (int k = 0; k < n; k++) {
perm[k] = k + 1;
}
int i = 0;
while (i < s.length()) {
// If we see an 'I', the current order is fine, move on.
if (s.charAt(i) == 'I') {
i++;
continue;
}
// If we see a 'D', it's the start of a decreasing sequence.
// Find the end of this consecutive 'D' block.
int j = i;
while (j + 1 < s.length() && s.charAt(j + 1) == 'D') {
j++;
}
// The 'D' block from s[i...j] corresponds to perm[i...j+1].
// We need to reverse this segment of the permutation.
// For example, s="D", i=0, j=0, reverse perm[0...1].
// For s="DD", i=0, j=1, reverse perm[0...2].
reverse(perm, i, j + 1);
// Move past the processed 'D' block.
i = j + 1;
}
return perm;
}
private void reverse(int[] arr, int start, int end) {
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
}
Find Permutation in C++
class Solution {
public:
vector<int> findPermutation(string s) {
int n = s.length() + 1;
vector<int> perm(n);
// Initialize perm with [1, 2, 3, ..., n]
iota(perm.begin(), perm.end(), 1);
int i = 0;
while (i < s.length()) {
// If we see an 'I', the current order is fine, move on.
if (s[i] == 'I') {
i++;
continue;
}
// If we see a 'D', it's the start of a decreasing sequence.
// Find the end of this consecutive 'D' block.
int j = i;
while (j + 1 < s.length() && s[j + 1] == 'D') {
j++;
}
// The 'D' block from s[i...j] corresponds to perm[i...j+1].
// We need to reverse this segment of the permutation.
// For example, s="D", i=0, j=0, reverse perm[0...1].
// For s="DD", i=0, j=1, reverse perm[0...2].
reverse(perm.begin() + i, perm.begin() + j + 2);
// Move past the processed 'D' block.
i = j + 1;
}
return perm;
}
};
Find Permutation in TypeScript
function findPermutation(s: string): number[] {
const n = s.length + 1;
// Initialize perm with [1, 2, 3, ..., n]
const perm: number[] = Array.from({ length: n }, (_, i) => i + 1);
const reverse = (arr: number[], start: number, end: number) => {
while (start < end) {
[arr[start], arr[end]] = [arr[end], arr[start]]; // Swap elements
start++;
end--;
}
};
let i = 0;
while (i < s.length) {
// If we see an 'I', the current order is fine, move on.
if (s[i] === 'I') {
i++;
continue;
}
// If we see a 'D', it's the start of a decreasing sequence.
// Find the end of this consecutive 'D' block.
let j = i;
while (j + 1 < s.length && s[j + 1] === 'D') {
j++;
}
// The 'D' block from s[i...j] corresponds to perm[i...j+1].
// We need to reverse this segment of the permutation.
// For example, s="D", i=0, j=0, reverse perm[0...1].
// For s="DD", i=0, j=1, reverse perm[0...2].
reverse(perm, i, j + 1);
// Move past the processed 'D' block.
i = j + 1;
}
return perm;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.