Skip to content

Commit 3e5bcb3

Browse files
committed
[LeetCode Sync] Runtime - 102 ms (65.62%), Memory - 32.7 MB (53.29%)
1 parent 864e94a commit 3e5bcb3

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<p>You are given a <strong>0-indexed</strong> string <code>s</code> of <strong>even</strong> length <code>n</code>. The string consists of <strong>exactly</strong> <code>n / 2</code> opening brackets <code>&#39;[&#39;</code> and <code>n / 2</code> closing brackets <code>&#39;]&#39;</code>.</p>
2+
3+
<p>A string is called <strong>balanced</strong> if and only if:</p>
4+
5+
<ul>
6+
<li>It is the empty string, or</li>
7+
<li>It can be written as <code>AB</code>, where both <code>A</code> and <code>B</code> are <strong>balanced</strong> strings, or</li>
8+
<li>It can be written as <code>[C]</code>, where <code>C</code> is a <strong>balanced</strong> string.</li>
9+
</ul>
10+
11+
<p>You may swap the brackets at <strong>any</strong> two indices <strong>any</strong> number of times.</p>
12+
13+
<p>Return <em>the <strong>minimum</strong> number of swaps to make </em><code>s</code> <em><strong>balanced</strong></em>.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> s = &quot;][][&quot;
20+
<strong>Output:</strong> 1
21+
<strong>Explanation:</strong> You can make the string balanced by swapping index 0 with index 3.
22+
The resulting string is &quot;[[]]&quot;.
23+
</pre>
24+
25+
<p><strong class="example">Example 2:</strong></p>
26+
27+
<pre>
28+
<strong>Input:</strong> s = &quot;]]][[[&quot;
29+
<strong>Output:</strong> 2
30+
<strong>Explanation:</strong> You can do the following to make the string balanced:
31+
- Swap index 0 with index 4. s = &quot;[]][][&quot;.
32+
- Swap index 1 with index 5. s = &quot;[[][]]&quot;.
33+
The resulting string is &quot;[[][]]&quot;.
34+
</pre>
35+
36+
<p><strong class="example">Example 3:</strong></p>
37+
38+
<pre>
39+
<strong>Input:</strong> s = &quot;[]&quot;
40+
<strong>Output:</strong> 0
41+
<strong>Explanation:</strong> The string is already balanced.
42+
</pre>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>n == s.length</code></li>
49+
<li><code>2 &lt;= n &lt;= 10<sup>6</sup></code></li>
50+
<li><code>n</code> is even.</li>
51+
<li><code>s[i]</code> is either <code>&#39;[&#39; </code>or <code>&#39;]&#39;</code>.</li>
52+
<li>The number of opening brackets <code>&#39;[&#39;</code> equals <code>n / 2</code>, and the number of closing brackets <code>&#39;]&#39;</code> equals <code>n / 2</code>.</li>
53+
</ul>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int minSwaps(string s) {
4+
int x = 0;
5+
6+
for (char c : s) {
7+
if (c == '[') x++;
8+
else if (x > 0) x--;
9+
}
10+
11+
return (x+1)/2;
12+
}
13+
};

0 commit comments

Comments
 (0)