[Leetcode] Find Permutation
By now, you are given a secret signature consisting of character ‘D’ and ‘I’. ‘D’ represents a decreasing relationship between two numbers, ‘I’ represents an increasing relationship between two numbers. And our secret signature was constructed by a special integer array, which contains uniquely all the different number from 1 to n (n is the length of the secret signature plus 1). For example, the secret signature “DI” can be constructed by array [2,1,3] or [3,1,2], but won’t be constructed by array [3,2,4] or [2,1,3,4], which are both illegal constructing special string that can’t represent the “DI” secret signature.
On the other hand, now your job is to find the lexicographically smallest permutation of [1, 2, … n] could refer to the given secret signature in the input.
Example 1:
|
1 2 3 |
Input: "I" Output: [1,2] Explanation: [1,2] is the only legal initial spectial string can construct secret signature "I", where the number 1 and 2 construct an increasing relationship. |
Example 2:
|
1 2 3 4 |
Input: "DI" Output: [2,1,3] Explanation: Both [2,1,3] and [3,1,2] can construct the secret signature "DI", but since we want to find the one with the smallest lexicographical permutation, you need to output [2,1,3] |
Note:
- The input string will only contain the character ‘D’ and ‘I’.
- The length of input string is a positive integer and will not exceed 10,000
A simple solution is to use dfs search and back tracking. See the following code:
|
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 41 42 43 44 45 46 47 48 |
public class Solution { public int[] findPermutation(String s) { int n = s.length() + 1; boolean[] visited = new boolean[n + 1]; int[] res = new int[n]; for(int i = 1; i <= n; i++) { visited[i] = true; res[0] = i; if(find(1, visited, s, res)) { return res; } visited[i] = false; } return new int[0]; } boolean find(int i, boolean[] visited, String s, int[] res) { if(i == visited.length - 1) { return true; } for(int k = 1; k < visited.length; k++) { if(visited[k]) continue; if(s.charAt(i - 1) == 'D') { if(k < res[i - 1]) { res[i] = k; visited[k] = true; if(find(i + 1, visited, s, res)) { return true; } visited[k] = false; res[i] = 0; } } if(s.charAt(i - 1) == 'I') { if(k > res[i - 1]) { res[i] = k; visited[k] = true; if(find(i + 1, visited, s, res)) { return true; } visited[k] = false; res[i] = 0; } } } return false; } } |
However, the above method does not work when the input is too long.
The following is a much better solution:
For example, given IDIIDD we start with sorted sequence 1234567
Then for each k continuous D starting at index i we need to reverse [i, i+k] portion of the sorted sequence.
|
1 2 3 |
IDIIDD 1234567 // sorted 1324765 // answer |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public int[] findPermutation(String s) { int n = s.length(), arr[] = new int[n + 1]; for (int i = 0; i <= n; i++) arr[i] = i + 1; // sorted for (int h = 0; h < n; h++) { if (s.charAt(h) == 'D') { int l = h; while (h < n && s.charAt(h) == 'D') h++; reverse(arr, l, h); } } return arr; } void reverse(int[] arr, int l, int h) { while (l < h) { arr[l] ^= arr[h]; arr[h] ^= arr[l]; arr[l] ^= arr[h]; l++; h--; } } |
https://discuss.leetcode.com/topic/76221/java-o-n-clean-solution-easy-to-understand











