Easy Java solution using Stacks. Beats 100% in memory ( O(n) ).
Intuition\n Describe your first thoughts on how to solve this problem. \nWe can maintain an ordered map for keeping all the counts of the lexicographically mini
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nWe can maintain an ordered map for keeping all the counts of the lexicographically minimum characters. Keep pushing all the characters onto a stack until you see a *. We we see a *, we can look up the minimum character as the first key of the map and keep popping elements on to another stack until we reach that element. Finally push all the elements back from the temp stack to the main stack.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe use a TreeMap to store the count of the characters in lexicographic order and keep two stacks, st and temp for main string and temporarily popping elements. Finally we convert our stack into a string.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ --> O(length^2)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --> O(length)\n\n# Code\n```\nclass Solution {\n    public String clearStars(String s) {\n        TreeMap<Character, Integer> map = new TreeMap<>();\n        Stack<Character> st = new Stack<>();\n        Stack<Character> temp = new Stack<>();\n        List<Integer> indices;\n        for(int i=0;i<s.length();i++){\n            if(s.charAt(i) == \'*\') {\n                char ch = map.firstKey();\n                while(st.size() > 0 && st.peek() != ch){\n                    temp.push(st.pop());\n                }\n                st.pop();\n                while(temp.size() > 0) st.push(temp.pop());\n                int count = map.get(ch);\n                if(count == 1) map.remove(ch);\n                else map.put(ch, count-1);\n            } else{\n                st.push(s.charAt(i));\n                map.put(s.charAt(i), map.getOrDefault(s.charAt(i),0)+1);\n            }\n        }\n        String ans = "";\n        while(st.size() > 0) ans = st.pop() + ans;\n        return ans;\n    }\n}\n```
['Java']