Skip to content

Commit 90ed9b2

Browse files
authored
Create solution.cpp
1 parent 871c1f4 commit 90ed9b2

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
int minLength(string s)
4+
{
5+
auto ABPos = s.find("AB");
6+
auto CDPos = s.find("CD");
7+
8+
while (ABPos != string::npos || CDPos != string::npos)
9+
{
10+
// erase either substrings one at a time
11+
if (ABPos != string::npos)
12+
{
13+
s.erase(ABPos, 2);
14+
}
15+
else if (CDPos != string::npos)
16+
{
17+
s.erase(CDPos, 2);
18+
}
19+
20+
// update positions
21+
ABPos = s.find("AB");
22+
CDPos = s.find("CD");
23+
}
24+
25+
return s.length();
26+
}
27+
};

0 commit comments

Comments
 (0)