Skip to content

484. Find Permutation 👍

Approach 1: Stack

  • Time: $O(n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
 public:
  vector<int> findPermutation(string s) {
    vector<int> ans;
    stack<int> stack;

    for (int i = 0; i < s.length(); ++i) {
      stack.push(i + 1);
      if (s[i] == 'I')
        while (!stack.empty())
          ans.push_back(stack.top()), stack.pop();
    }
    stack.push(s.length() + 1);

    while (!stack.empty())
      ans.push_back(stack.top()), stack.pop();

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
  public int[] findPermutation(String s) {
    int[] ans = new int[s.length() + 1];
    int ansIndex = 0;
    Deque<Integer> stack = new ArrayDeque<>();

    for (int i = 0; i < s.length(); ++i) {
      stack.push(i + 1);
      if (s.charAt(i) == 'I')
        while (!stack.isEmpty())
          ans[ansIndex++] = stack.pop();
    }
    stack.push(s.length() + 1);

    while (!stack.isEmpty())
      ans[ansIndex++] = stack.pop();

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution:
  def findPermutation(self, s: str) -> list[int]:
    ans = []
    stack = []

    for i, c in enumerate(s):
      stack.append(i + 1)
      if c == 'I':
        while stack:  # Consume all decreasings
          ans.append(stack.pop())
    stack.append(len(s) + 1)

    while stack:
      ans.append(stack.pop())

    return ans

Approach 2: Reverse each group

  • Time: $O(n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
 public:
  vector<int> findPermutation(string s) {
    vector<int> ans(s.length() + 1);

    iota(ans.begin(), ans.end(), 1);

    // For each D* group (s[i..j]), reverse ans[i..j + 1].
    int i = -1;
    int j = -1;

    while (true) {
      i = getNextIndex(s, 'D', j + 1);
      if (i == s.length())
        break;
      j = getNextIndex(s, 'I', i + 1);
      reverse(ans.begin() + i, ans.begin() + j + 1);
    }

    return ans;
  }

 private:
  int getNextIndex(const string& s, char c, int start) {
    for (int i = start; i < s.length(); ++i)
      if (s[i] == c)
        return i;
    return s.length();
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
  public int[] findPermutation(String s) {
    int[] ans = new int[s.length() + 1];

    for (int i = 0; i < ans.length; ++i)
      ans[i] = i + 1;

    // For each D* group (s[i..j]), reverse ans[i..j + 1].
    int i = -1;
    int j = -1;

    while (true) {
      i = getNextIndex(s, 'D', j + 1);
      if (i == s.length())
        break;
      j = getNextIndex(s, 'I', i + 1);
      reverse(ans, i, j);
    }

    return ans;
  }

  private int getNextIndex(final String s, char c, int start) {
    for (int i = start; i < s.length(); ++i)
      if (s.charAt(i) == c)
        return i;
    return s.length();
  }

  private void reverse(int[] A, int l, int r) {
    while (l < r)
      swap(A, l++, r--);
  }

  private void swap(int[] A, int i, int j) {
    final int temp = A[i];
    A[i] = A[j];
    A[j] = temp;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution:
  def findPermutation(self, s: str) -> list[int]:
    ans = [i for i in range(1, len(s) + 2)]

    # For each D* group (s[i..j]), reverse ans[i..j + 1].
    i = -1
    j = -1

    def getNextIndex(c: str, start: int) -> int:
      for i in range(start, len(s)):
        if s[i] == c:
          return i
      return len(s)

    while True:
      i = getNextIndex('D', j + 1)
      if i == len(s):
        break
      j = getNextIndex('I', i + 1)
      ans[i:j + 1] = ans[i:j + 1][::-1]

    return ans