O(N) using stack
Maintain an array of stacks where each element will contain a stack of indices of its occurence.\nRemove the last index of the smallest character whenever encou
Maintain an array of stacks where each element will contain a stack of indices of its occurence.\nRemove the last index of the smallest character whenever encounter a \'*\' .\nWe are removing last index because we want to make lexi. smallest string so we want to keep smallest characters in front of the string.\n\n```\nclass Solution {\n    public String clearStars(String str) {\n        char a[] = str.toCharArray();\n        Stack<Integer>[] s = new Stack[26];\n        for(int i=0;i<26;i++) s[i] = new Stack<>();\n        for(int i=0;i<a.length;i++) {\n            if(a[i] == \'*\') {\n                for(int j=0;j<26;j++) {\n                    if(!s[j].isEmpty()) {\n                        int index = s[j].pop();\n                        a[index] = \'*\';\n                        break;\n                    }\n                }\n            }else{\n                s[a[i]-\'a\'].push(i);\n            }\n        }\n        StringBuilder sb = new StringBuilder();\n        for(char ch : a) if(ch !=\'*\') sb.append(ch);\n        return sb.toString();\n    }\n}\n```
[]