✅☑[C++] || Without Stack || EXPLAINED🔥
PLEASE UPVOTE IF IT HELPED\n\n#### Same Approach in this question too: https://leetcode.com/problems/remove-duplicate-letters/solutions/4093499/c-without-stack-
# *PLEASE UPVOTE IF IT HELPED*\n\n#### **Same Approach in this question too:** https://leetcode.com/problems/remove-duplicate-letters/solutions/4093499/c-without-stack-explained/\n\n---\n\n# Approach\n\n***(Also explained in the code)***\n\n#### **Step-by-Step Explanation:**\n\n1. Initialize two vectors, `count` and `used`, and an empty string `result`.\n\n   - `count`: Stores the frequency of each character in the input string.\n   - `used`: Keeps track of whether a character has been used in the result.\n   - `result`: Will store the final lexicographically smallest string without duplicate letters.\n\n2. Iterate through the characters of the input string `s`.\n\n   - For each character ch:\n     - Decrement its frequency in the count vector.\n\n3. Check if the character `ch` is already used in the result.\n\n    - If it\'s used, skip processing it.\n\n4. Enter a loop to remove characters from the result string if they are greater than the current character ch in lexicographical order.\n\n   - This loop continues until one of the following conditions is met:\n     - The `result` string is empty.\n     - The last character in the `result` string is smaller than `ch`.\n      - The count of the last character in the `count` vector is greater than zero.\n1. Inside the loop, mark the removed character as unused in the `used` vector and remove it from the `result` string.\n\n1. Add the current character `ch` to the `result` string and mark it as `used` in the used vector.\n\n1. After processing all characters, return the `result` string, which contains the lexicographically smallest string without duplicate letters.\n\n---\n\n\n\n# Complexity\n- **Time complexity:**\n$$O(n)$$\n\n- **Space complexity:**\n$$O(1)$$\n\n---\n\n\n\n# Code\n```\nclass Solution {\npublic:\n    string removeDuplicateLetters(string s) {\n        vector<int> count(26, 0); // Count the frequency of each character\n        vector<bool> used(26, false); // Track if a character is already used\n        string result = ""; // To store the final result\n\n        for (char ch : s) {\n            count[ch - \'a\']++; // Increment the frequency of the current character\n        }\n\n        for (char ch : s) {\n            count[ch - \'a\']--; // Decrement the frequency of the current character\n\n            // Check if the character is already in the result or not\n            if (used[ch - \'a\']) {\n                continue; // Skip if it\'s already used\n            }\n\n            // Remove characters from the result string if they are greater than the current character\n            while (!result.empty() && ch < result.back() && count[result.back() - \'a\'] > 0) {\n                used[result.back() - \'a\'] = false;\n                result.pop_back();\n            }\n\n            result.push_back(ch); // Add the current character to the result\n            used[ch - \'a\'] = true; // Mark it as used\n        }\n\n        return result;\n    }\n};\n\n```\n# *PLEASE UPVOTE IF IT HELPED*\n\n\n---\n---\n\n
['String', 'Stack', 'Greedy', 'Monotonic Stack', 'C++']