Skip to content

Commit abc309a

Browse files
committed
Time: 94 ms (88.68%), Space: 32.6 MB (91.34%) - LeetHub
1 parent 2a57eab commit abc309a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int minSwaps(string s) {
4+
int cnt = (s.front() == ']');
5+
int two = 0;
6+
int open = !cnt;
7+
for (int i = 1; i < s.size(); i++) {
8+
if (s[i] == ']') {
9+
if (open) {
10+
open--;
11+
continue;
12+
}
13+
cnt++;
14+
if (i & 1) {
15+
if (s[i - 1] == s[i]) {
16+
two++;
17+
}
18+
}
19+
} else {
20+
open++;
21+
}
22+
}
23+
return cnt - two;
24+
}
25+
};

0 commit comments

Comments
 (0)