2696.cpp (504B)
1 class Solution { 2 public: 3 int minLength(string s) const { 4 int n = size(s); 5 while (true) { 6 int j = 0; 7 for (int i = 0; i < n; i++) { 8 if (s[i] == 'A' && s[i + 1] == 'B') 9 i++; 10 else if (s[i] == 'C' && s[i + 1] == 'D') 11 i++; 12 else 13 s[j++] = s[i]; 14 } 15 16 if (j == n) break; 17 s[n = j] = '\0'; 18 } 19 20 return n; 21 } 22 };