Skip to content

Commit 14c0b92

Browse files
committed
[LeetCode Sync] Runtime - 0 ms (100.00%), Memory - 11.6 MB (62.71%)
1 parent 97474cf commit 14c0b92

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p>You are given a string <code>s</code> consisting only of <strong>uppercase</strong> English letters.</p>
2+
3+
<p>You can apply some operations to this string where, in one operation, you can remove <strong>any</strong> occurrence of one of the substrings <code>&quot;AB&quot;</code> or <code>&quot;CD&quot;</code> from <code>s</code>.</p>
4+
5+
<p>Return <em>the <strong>minimum</strong> possible length of the resulting string that you can obtain</em>.</p>
6+
7+
<p><strong>Note</strong> that the string concatenates after removing the substring and could produce new <code>&quot;AB&quot;</code> or <code>&quot;CD&quot;</code> substrings.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre>
13+
<strong>Input:</strong> s = &quot;ABFCACDB&quot;
14+
<strong>Output:</strong> 2
15+
<strong>Explanation:</strong> We can do the following operations:
16+
- Remove the substring &quot;<u>AB</u>FCACDB&quot;, so s = &quot;FCACDB&quot;.
17+
- Remove the substring &quot;FCA<u>CD</u>B&quot;, so s = &quot;FCAB&quot;.
18+
- Remove the substring &quot;FC<u>AB</u>&quot;, so s = &quot;FC&quot;.
19+
So the resulting length of the string is 2.
20+
It can be shown that it is the minimum length that we can obtain.</pre>
21+
22+
<p><strong class="example">Example 2:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> s = &quot;ACBBD&quot;
26+
<strong>Output:</strong> 5
27+
<strong>Explanation:</strong> We cannot do any operations on the string so the length remains the same.
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= s.length &lt;= 100</code></li>
35+
<li><code>s</code>&nbsp;consists only of uppercase English letters.</li>
36+
</ul>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int minLength(string s) {
4+
stack<char> st;
5+
6+
for (char c : s) {
7+
if (!st.empty() && c == 'B' && st.top() == 'A') st.pop();
8+
else if (!st.empty() && c == 'D' && st.top() == 'C') st.pop();
9+
10+
else st.push(c);
11+
}
12+
13+
return st.size();
14+
}
15+
};

0 commit comments

Comments
 (0)