Java O(n) O(1) (beats 100%)
```\n// Solution - Time Complexity O(n) and space complexity O(1) (Here we have used arrays of length 26 making it of constant space)\n        \n        //if le
```\n// Solution - Time Complexity O(n) and space complexity O(1) (Here we have used arrays of length 26 making it of constant space)\n        \n        //if length of the string is 1 or 0 return that string\n        if(s.length()<=1)\n            return s;\n        \n        int len=s.length();\n        // This will hold our current unique element\n        boolean[] seen=new boolean[26];\n        \n        // This will hold our every character\'s of string last index  \n        int[] lastOccurence=new int[26];\n        \n        // First add all the elements in the lastOccurence array with its last index\n        for(int i=0;i<len;i++){\n            lastOccurence[s.charAt(i)-\'a\']=i;\n        }\n        \n        // This will hold our final string characters\n        StringBuilder sb=new StringBuilder();\n        \n        // Now we will check for each character from the start\n        for(int i=0;i<len;i++){\n            char c=s.charAt(i);\n            \n            //if the current character is not seen in the seen array\n            if(!seen[c-\'a\']){\n                int currLen=sb.length();\n                \n                // loop till the current character is less than other elements in our already made final string provided they have to be seen later (this we will check through lastOccurence array for each character\'s last occuring index in the string)\n                while(currLen>0 && c<sb.charAt(currLen-1) && lastOccurence[sb.charAt(currLen-1)-\'a\']>i){\n                    // if condition satisifes remove the other characters from our final string accordingly as if it is not seen yet \n                    // mark all to be removed characters as unseen\n                    seen[sb.charAt(currLen-1)-\'a\']=false;\n                    // remove that characters from our final string\n                    sb.deleteCharAt(currLen-1);\n                    //set the new length of our final string\n                    currLen=sb.length();\n                }\n                //append the current character\n                sb.append(c);\n                // marked it as seen\n                seen[c-\'a\']=true;\n            }\n            \n        }\n        \n        return sb.toString();\n
[]