484.Find Permutation
1- Greedy Using Stack
Can also use reverse array
Two pointer
https://leetcode.com/problems/find-permutation/solution/
class Solution {
public int[] findPermutation(String s) {
int[] ans = new int[s.length() + 1];
Deque<Integer> stack = new ArrayDeque<>();
int ansIndex = 0;
for(int i = 1; i <= s.length(); i++){
if(s.charAt(i - 1) == 'I'){
stack.push(i);
while(!stack.isEmpty()){
ans[ansIndex] = stack.pop();
ansIndex++;
}
}
else{
stack.push(i);
}
}
stack.push(s.length() + 1);
while(!stack.isEmpty()){
ans[ansIndex++] = stack.pop();
}
return ans;
}
}