We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 871c1f4 commit 90ed9b2Copy full SHA for 90ed9b2
MinimumStringLengthAfterRemovingSubstrings/solution.cpp
@@ -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