# 去除重复字母

给你一个字符串 s ,请你去除字符串中重复的字母,使得每个字母只出现一次。需保证 返回结果的字典序最小(要求不能打乱其他字符的相对位置)。

注意:该题与 1081 https://leetcode-cn.com/problems/smallest-subsequence-of-distinct-characters 相同

 

示例 1:

输入:s = "bcabc"
输出"abc"

示例 2:

输入:s = "cbacdcbc"
输出:"acdb"

 

提示:

以下错误的选项是?

## aop ### before ```c #include using namespace std; ``` ### after ```c int main() { Solution sol; string s = "cbacdcbc"; string res = sol.removeDuplicateLetters(s); cout << res; return 0; } ``` ## 答案 ```c class Solution { public: string removeDuplicateLetters(string s) { int count[26] = {0}; for (int i = 0; i < s.length(); i++) count[s[i] - 'a']++; int pos = 0; for (int i = 0; i < s.length(); i++) { if (s[i] < s[pos]) pos = i; if (count[s[i] - 'a'] == 0) break; } string suffix; if (pos + 1 > s.length()) suffix = ""; else suffix = s.substr(pos + 1, s.length() - pos - 1); return s.length() == 0 ? "" : s[pos] + removeDuplicateLetters(removeOneLetter(suffix, s[pos])); } string removeOneLetter(string s, char ch) { while (1) { int index = s.find(ch); if (index != -1) s = s.erase(index, 1); else break; } return s; } }; ``` ## 选项 ### A ```c class Solution { public: string removeDuplicateLetters(string s) { unordered_map mp; unordered_map isIn; int len = s.length(); for (int i = 0; i < len; i++) { mp[s[i]]++; isIn[s[i]] = false; } stack ans; for (int i = 0; i < len; i++) { if (ans.empty()) { ans.push(s[i]); isIn[s[i]] = true; } else if (!isIn[s[i]]) { while (!ans.empty() && s[i] < ans.top()) { if (mp[ans.top()]) { isIn[ans.top()] = false; ans.pop(); } else break; } ans.push(s[i]); isIn[s[i]] = true; } mp[s[i]]--; } string ansS; while (!ans.empty()) { ansS += ans.top(); ans.pop(); } reverse(ansS.begin(), ansS.end()); return ansS; } }; ``` ### B ```c class Solution { public: string removeDuplicateLetters(string s) { string ans; for (int i = 0; i < s.size(); i++) { if (ans.find(s[i]) != string::npos) continue; for (int j = ans.length() - 1; j >= 0; j--) { if (ans[j] > s[i] && s.find(ans[j], i + 1) != string::npos) { ans.erase(j); continue; } break; } ans += s[i]; } return ans; } }; ``` ### C ```c class Solution { public: string res = "0"; string removeDuplicateLetters(string s) { int count[26] = {0}; int visited[26] = {0}; for (int i = 0; i < s.size(); ++i) ++count[s[i] - 'a']; for (const auto &c : s) { --count[c - 'a']; if (visited[c - 'a'] == 1) continue; while (c < res.back() && count[res.back() - 'a']) { visited[res.back() - 'a'] = 0; res.pop_back(); } res += c; visited[c - 'a'] = 1; } return res.substr(1); } }; ```