1081. Smallest Subsequence of Distinct Characters LeetCode Solution

In this guide, you will get 1081. Smallest Subsequence of Distinct Characters LeetCode Solution with the best time and space complexity. The solution to Smallest Subsequence of Distinct Characters problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Smallest Subsequence of Distinct Characters solution in C++
  4. Smallest Subsequence of Distinct Characters solution in Java
  5. Smallest Subsequence of Distinct Characters solution in Python
  6. Additional Resources
1081. Smallest Subsequence of Distinct Characters LeetCode Solution image

Problem Statement of Smallest Subsequence of Distinct Characters

Given a string s, return the lexicographically smallest subsequence of s that contains all the distinct characters of s exactly once.

Example 1:

Input: s = “bcabc”
Output: “abc”

Example 2:

Input: s = “cbacdcbc”
Output: “acdb”

Constraints:

1 <= s.length <= 1000
s consists of lowercase English letters.

Note: This question is the same as 316: https://leetcode.com/problems/remove-duplicate-letters/

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

1081. Smallest Subsequence of Distinct Characters LeetCode Solution in C++

class Solution {
 public:
  string smallestSubsequence(string text) {
    string ans;
    vector<int> count(128);
    vector<bool> used(128);

    for (const char c : text)
      ++count[c];

    for (const char c : text) {
      --count[c];
      if (used[c])
        continue;
      while (!ans.empty() && ans.back() > c && count[ans.back()] > 0) {
        used[ans.back()] = false;
        ans.pop_back();
      }
      used[c] = true;
      ans.push_back(c);
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

1081. Smallest Subsequence of Distinct Characters LeetCode Solution in Java

class Solution {
  public String smallestSubsequence(String text) {
    StringBuilder sb = new StringBuilder();
    int[] count = new int[128];
    boolean[] used = new boolean[128];

    for (final char c : text.toCharArray())
      ++count[c];

    for (final char c : text.toCharArray()) {
      --count[c];
      if (used[c])
        continue;
      while (sb.length() > 0 && last(sb) > c && count[last(sb)] > 0) {
        used[last(sb)] = false;
        sb.setLength(sb.length() - 1);
      }
      used[c] = true;
      sb.append(c);
    }

    return sb.toString();
  }

  private char last(StringBuilder sb) {
    return sb.charAt(sb.length() - 1);
  }
}
// code provided by PROGIEZ

1081. Smallest Subsequence of Distinct Characters LeetCode Solution in Python

class Solution:
  def smallestSubsequence(self, text: str) -> str:
    ans = []
    count = collections.Counter(text)
    used = [False] * 26

    for c in text:
      count[c] -= 1
      if used[string.ascii_lowercase.index(c)]:
        continue
      while ans and ans[-1] > c and count[ans[-1]] > 0:
        used[ord(ans[-1]) - ord('a')] = False
        ans.pop()
      ans.append(c)
      used[ord(ans[-1]) - ord('a')] = True

    return ''.join(ans)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.