Remove Duplicate Letters LeetCode Solution

Last updated on October 6th, 2024 at 02:02 pm

Here, We see Remove Duplicate Letters LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Topics

Greedy, Stack

Companies

Google

Level of Question

Medium

Remove Duplicate Letters LeetCode Solution

Remove Duplicate Letters LeetCode Solution

Problem Statement

Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.

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

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

1. Remove Duplicate Letters Leetcode Solution C++

class Solution {
public:
    string removeDuplicateLetters(string s) {
        vector<int> cnt(26,0)  , vis(26,0);
        string res = "";
        int n = s.size();
        for(int i = 0; i<n; ++i)
            cnt[s[i] - 'a']++;
        for(int i = 0; i<n; ++i)
        {
            cnt[s[i] - 'a']--;
            if(!vis[s[i]- 'a'])
            {
                while(res.size() > 0 && res.back() > s[i] && cnt[res.back() - 'a'] > 0)
                {
                    vis[res.back() - 'a'] = 0;
                    res.pop_back();
                }
                res += s[i];
                vis[s[i] - 'a'] = 1;
            }
        }
        return res;
    }
};

2. Remove Duplicate Letters Leetcode Solution Java

class Solution {
    public String removeDuplicateLetters(String s) {
        Stack<Character> stack = new Stack<>();
        Set<Character> seen = new HashSet<>();
        Map<Character, Integer> lastOcc = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            lastOcc.put(s.charAt(i), i);
        }
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!seen.contains(c)) {
                while (!stack.isEmpty() && c < stack.peek() && i < lastOcc.get(stack.peek())) {
                    seen.remove(stack.pop());
                }
                seen.add(c);
                stack.push(c);
            }
        }
        StringBuilder result = new StringBuilder();
        for (char c : stack) {
            result.append(c);
        }
        return result.toString();
    }
}

3. Remove Duplicate Letters Solution JavaScript

var removeDuplicateLetters = function(s) {
    let stack = [];
    let seen = new Set();
    let lastOcc = {};
    for (let i = 0; i < s.length; i++) {
        lastOcc[s[i]] = i;
    }
    for (let i = 0; i < s.length; i++) {
        let c = s[i];
        if (!seen.has(c)) {
            while (stack.length && c < stack[stack.length - 1] && i < lastOcc[stack[stack.length - 1]]) {
                seen.delete(stack.pop());
            }
            seen.add(c);
            stack.push(c);
        }
    }
    return stack.join('');
}

4. Remove Duplicate Letters Solution Python

class Solution(object):
    def removeDuplicateLetters(self, s):
        stack = []
        seen = set() 
        last_occ = {c: i for i, c in enumerate(s)}
        for i, c in enumerate(s):
            if c not in seen:
                while stack and c < stack[-1] and i < last_occ[stack[-1]]:
                    seen.discard(stack.pop())
                seen.add(c)
                stack.append(c)
        return ''.join(stack)
Scroll to Top