🚀 Optimized Solution - Smallest Subsequence of Distinct Characters (Beats 100% in All Languages)
IntuitionThe goal is to find the smallest lexicographical order string by removing duplicate letters while maintaining the relative order of characters.
To achi
# Intuition
The goal is to find the smallest lexicographical order string by removing duplicate letters while maintaining the relative order of characters.  
To achieve this, we use a **monotonic stack** approach to ensure that the resultant string is in sorted order and contains only unique letters.

# Approach
1. **Frequency Count:** Maintain a frequency array `freq` to count occurrences of each character.  
2. **Visited Set:** Use a boolean array `vis` (or a set in Python) to track which characters are already in the result.  
3. **Monotonic Stack:**  
   - Iterate over the string.  
   - If the current character is already in the result (`vis` is true), decrement its frequency and continue.  
   - Otherwise, check the top of the stack:
     - If the top character is **greater than the current character** and **it appears later in the string (freq > 0)**, remove it from the stack.  
     - Mark the removed character as unvisited.  
   - Push the current character onto the stack and mark it as visited.  
4. **Result Construction:**  
   - Pop elements from the stack to construct the final answer in the correct order.  

# Complexity
- **Time Complexity:**  
  - $$O(n)$$ (Each character is pushed and popped from the stack at most once.)
- **Space Complexity:**  
  - $$O(1)$$ (We use fixed-size arrays for `freq` and `vis`, and the stack holds at most 26 characters.)

---

# Code Implementations

```cpp []

class Solution {
public:
    string smallestSubsequence(string s) {
        vector<int> freq(26, 0);
        vector<bool> vis(26, false);
        stack<char> st;

        for (char i : s) freq[i - 'a']++;

        for (char i : s) {
            if (vis[i - 'a']) {
                freq[i - 'a']--;
                continue;
            }
            while (!st.empty() && st.top() > i && freq[st.top() - 'a'] > 0) {
                vis[st.top() - 'a'] = false;
                st.pop();
            }
            st.push(i);
            vis[i - 'a'] = true;
            freq[i - 'a']--;
        }

        string ans = "";
        while (!st.empty()) {
            ans = st.top() + ans;
            st.pop();
        }

        return ans;
    }
};
```
```Java []
import java.util.*;

class Solution {
    public String smallestSubsequence(String s) {
        int[] freq = new int[26];
        boolean[] vis = new boolean[26];
        Stack<Character> stack = new Stack<>();

        for (char ch : s.toCharArray()) freq[ch - 'a']++;

        for (char ch : s.toCharArray()) {
            freq[ch - 'a']--;
            if (vis[ch - 'a']) continue;
            while (!stack.isEmpty() && stack.peek() > ch && freq[stack.peek() - 'a'] > 0) {
                vis[stack.pop() - 'a'] = false;
            }
            stack.push(ch);
            vis[ch - 'a'] = true;
        }

        StringBuilder ans = new StringBuilder();
        while (!stack.isEmpty()) {
            ans.insert(0, stack.pop());
        }

        return ans.toString();
    }
}

```
```Python []
class Solution:
    def smallestSubsequence(self, s: str) -> str:
        freq = {ch: 0 for ch in s}
        vis = set()
        stack = []

        for ch in s:
            freq[ch] += 1

        for ch in s:
            freq[ch] -= 1
            if ch in vis:
                continue
            while stack and stack[-1] > ch and freq[stack[-1]] > 0:
                vis.remove(stack.pop())
            stack.append(ch)
            vis.add(ch)

        return "".join(stack)

```

['Hash Table', 'String', 'Stack', 'C++', 'Java', 'Python3']