Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

316. Remove Duplicate Letters #566

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions C++/316_Remove_Duplicate_Letters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Name: Rajesh Sharma
// Date: 26/10/2022

/*
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
*/

// This question is the same as 1081: https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/
// That is language variation.

/*
Approach: As the question suggests, We can greedily pick the characters which affect the answer
and can be picked in future where they may be coming after lower lexicographical characters.

Time Complexity: O(n)
Space Complexity: O(n)
*/

class Solution {
public:
string smallestSubsequence(string s) {
vector < bool > vis(26, false); // visited array to mark if char already added to answer
vector < int > lastOccurance(26, 1e5); // last occurance of each character is stored in this, useful to check if we can ignore this char now and pick it later
for (int i = 0; i < s.length(); i++) lastOccurance[s[i] - 'a'] = i;
stack < char > st; // stack to access previous character
for (int i = 0; i < s.length(); i++) {
char c = s[i];
if (vis[c - 'a']) continue;
if (st.size() == 0) { // stack size is zero
st.push(c);
vis[c - 'a'] = true;
continue;
}

while (st.size() && st.top() > c && lastOccurance[st.top() - 'a'] > i) { // while our previous character is > us and can be ignored right now, we pop it from the stack
vis[st.top() - 'a'] = false;
st.pop();
}
st.push(c);
vis[c - 'a'] = true;
}
string ans = "";
while (st.size()) { //fill stack chars in the answer string, note they will have to be reversed.
ans.push_back(st.top());
st.pop();
}
reverse(ans.begin(), ans.end()); // reversing
return ans;
}
};