solution.md 4.5 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
# 去除重复字母
<p>给你一个字符串 <code>s</code> ,请你去除字符串中重复的字母,使得每个字母只出现一次。需保证 <strong>返回结果的字典序最小</strong>(要求不能打乱其他字符的相对位置)。</p>

<p><strong>注意:</strong>该题与 1081 <a href="https://leetcode-cn.com/problems/smallest-subsequence-of-distinct-characters">https://leetcode-cn.com/problems/smallest-subsequence-of-distinct-characters</a> 相同</p>

<p> </p>

<p><strong>示例 1:</strong></p>

<pre>
<strong>输入:</strong><code>s = "bcabc"</code>
<strong>输出<code></code></strong><code>"abc"</code>
</pre>

<p><strong>示例 2:</strong></p>

<pre>
<strong>输入:</strong><code>s = "cbacdcbc"</code>
<strong>输出:</strong><code>"acdb"</code></pre>

<p> </p>

<p><strong>提示:</strong></p>

<ul>
	<li><code>1 <= s.length <= 10<sup>4</sup></code></li>
	<li><code>s</code> 由小写英文字母组成</li>
</ul>

<p>以下错误的选项是?</p>

## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
    Solution sol;
    string s = "cbacdcbc";
    string res = sol.removeDuplicateLetters(s);
    cout << res;

    return 0;
}
```

## 答案
```cpp

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
```cpp
class Solution
{
public:
    string removeDuplicateLetters(string s)
    {
        unordered_map<char, int> mp;
        unordered_map<char, bool> isIn;
        int len = s.length();
        for (int i = 0; i < len; i++)
        {
            mp[s[i]]++;
            isIn[s[i]] = false;
        }
        stack<char> 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
```cpp
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
```cpp

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);
    }
};
```