O(N^2) solution with bitmasks
For each position save set with all characters after that position and\nsave set with needed characters. And in each iteration find the minimal\ncharacter that 
For each position save set with all characters after that position and\nsave set with needed characters. And in each iteration find the minimal\ncharacter that has set with all needed characters and erase the needed character.\n```\n    bool isSubset(int big, int small) {\n        for (int i = 0; i < 30; ++i) {\n            if ((small & (1 << i)) && !((big & (1 << i)))) {\n                return false;\n            }\n        }\n        return true;\n    }\n    \n    string smallestSubsequence(string text) {\n        int all = 0;\n        string T;\n        map<int, int> M;\n\n        T = text;\n        for (char c : text) all |= (1 << (c - \'a\'));\n        \n        for (int i = 0 ; i < T.size(); ++i) {\n            M[i] = 0;\n            for (int j = i; j < T.size(); ++j) {\n                M[i] |= (1 << (T[j] - \'a\'));\n            }\n        }\n        \n        string res;\n        int i = 0;\n        while (all) {\n            while (!(all & (1 << (T[i] - \'a\')))) i++;\n            int best = i;\n            for (int j = i; j < T.size(); ++j) {\n                if (!(all & (1 << (T[j] - \'a\')))) continue;\n                if (T[j] < T[best] && isSubset(M[j],all)) {\n                    best = j;\n                }\n            }\n            res.push_back(T[best]);\n            all &= ~(1 << (T[best] - \'a\'));\n            i = best;\n        }\n        return res;\n    }\n```
[]