Clean 23ms Stack solution


  • 2
    S
    public int[] findPermutation(String s) {
    	int[] res = new int[s.length()+1];
    	Stack<Integer> stack = new Stack<Integer>();
    	int min = 0;
    	for (int i = 0; i < s.length(); i++) {
    		char c = s.charAt(i);
    		if (c == 'D') stack.add(i);
    		else {
    			res[i] = ++min;
    			while (!stack.isEmpty()) res[stack.pop()] = ++min;
    		}
    	}
    	if (s.charAt(s.length()-1) == 'D') stack.add(s.length());
    	else res[res.length-1] = ++min;
    
    	while (!stack.isEmpty()) res[stack.pop()] = ++min;
    	return res;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.