- class Solution {
- public String smallestSubsequence(String text) {
- Stack<Character> stack = new Stack<>();
- char[] cs = text.toCharArray();
- int len = cs.length;
- char temp = 0;
- String sub = null;
- for(int i=0;i<len;i++) {
- if(i==0) {
- stack.push(cs[i]);
- }else {
- if(!stack.contains(cs[i])) {
- temp = stack.peek();
- sub = text.substring(i+1);
- while(cs[i]<temp&&sub.indexOf(temp)!=-1) {
- stack.pop();
- if(stack.empty()){
- break;
- }
- temp = stack.peek();
- }
- stack.push(cs[i]);
- }
- }
- }
- int stackLen = stack.size();
- char[] strD = new char[stackLen];
- for(int i=0;i<stackLen;i++) {
- strD[stackLen-1-i]=stack.pop();
- }
- return new String(strD);
- }
- }
来源: http://www.bubuko.com/infodetail-3098905.html