C++ Solution with Detailed Explanation
Problem is to remove duplicates and output a string with unique characters while maintaing the smallest(lexicographically) permutation of all possible subsequen
Problem is to remove duplicates and output a string with unique characters while maintaing the smallest(lexicographically) permutation of all possible subsequences of distinct characters.\n\n1. We have to just keep adding the unique characters to the answer and while adding them we have to check if the character to be added is smaller than the character which we have added previuosly to the answer. \n2. If it is smaller, then we can check whether the previous character is available for use in future iterations ? If yes, then we can just remove the character we added previously. Repeat this step, until we find a previous character smaller than current character.\n3. We can now add the current character to the answer.\n\n```\n class Solution {\n  public:\n      string smallestSubsequence(string text)\n      {\n          int last[26] = {};\n          int seen[26] = {};\n          int n = (int)text.size();\n          string ans="";\n          for(int i=0;i<n;i++)\n              last[text[i]-\'a\'] = i; // In order to know the last occurence of a character(step 2 above).At any point if we want to know that a given character is available in the future iterations.\n          \n          for(int i=0;i<n;i++)\n          {\n              if(seen[text[i]-\'a\'])\n                  continue;\n              seen[text[i]-\'a\']++; // To avoid adding duplicates\n              while(!ans.empty() and ans.back()>text[i] and i<last[ans.back()-\'a\']) // As explained in step 2 above.\n              {\n                  seen[ans.back()-\'a\'] = 0; // We have to reset this character in seen to 0 because when it comes again in future, it has to be added to the answer again.\n                  ans.pop_back();\n              }\n              \n              ans.push_back(text[i]);\n          }\n          return ans;\n      }\n};\n\n\n```
[]