Skip to content

Commit 6d16d6b

Browse files
Sync LeetCode submission Runtime - 3 ms (81.54%), Memory - 13.7 MB (58.07%)
1 parent 967a16b commit 6d16d6b

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<p>You are given a string <code>s</code> consisting of lowercase English letters. A <strong>duplicate removal</strong> consists of choosing two <strong>adjacent</strong> and <strong>equal</strong> letters and removing them.</p>
2+
3+
<p>We repeatedly make <strong>duplicate removals</strong> on <code>s</code> until we no longer can.</p>
4+
5+
<p>Return <em>the final string after all such duplicate removals have been made</em>. It can be proven that the answer is <strong>unique</strong>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> s = &quot;abbaca&quot;
12+
<strong>Output:</strong> &quot;ca&quot;
13+
<strong>Explanation:</strong>
14+
For example, in &quot;abbaca&quot; we could remove &quot;bb&quot; since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is &quot;aaca&quot;, of which only &quot;aa&quot; is possible, so the final string is &quot;ca&quot;.
15+
</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> s = &quot;azxxzy&quot;
21+
<strong>Output:</strong> &quot;ay&quot;
22+
</pre>
23+
24+
<p>&nbsp;</p>
25+
<p><strong>Constraints:</strong></p>
26+
27+
<ul>
28+
<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
29+
<li><code>s</code> consists of lowercase English letters.</li>
30+
</ul>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
string removeDuplicates(string s) {
4+
string t;
5+
for (char c : s) {
6+
if (!t.empty() && c == t.back()) t.pop_back();
7+
else t.push_back(c);
8+
}
9+
return t;
10+
}
11+
};

0 commit comments

Comments
 (0)