Beats 100% java users | solved using Tree set
\n\n# Complexity\n- Time complexity: O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(n)\n Add your space complexity here, e.g. O(n) \n
\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public static String clearStars(String s) {\n        TreeSet<Character> set = new TreeSet<>();\n        StringBuilder str = new StringBuilder();\n        int[] freq = new int[26];\n\n        for (char c : s.toCharArray()) {\n            if (c == \'*\') {\n                remove(str, set, freq);\n            } else {\n                freq[c - \'a\']++;\n                set.add(c);\n                str.append(c);\n            }\n        }\n        return str.toString();\n    }\n\n    public static void remove(StringBuilder sb, TreeSet<Character> set, int[] freq) {\n        if (sb.length() == 0) return;\n\n        char ch = set.first();\n        int lastIndex = sb.lastIndexOf(String.valueOf(ch));\n\n        if (lastIndex != -1) {\n            int remainingFrequency = --freq[ch - \'a\'];\n\n            if (remainingFrequency == 0) {\n                set.pollFirst();\n            }\n            sb.deleteCharAt(lastIndex);\n        }\n    }\n}\n```
['Ordered Set', 'Java']