1653.cpp (497B)
1 class Solution { 2 public: 3 int minimumDeletions(const string &s) const { 4 static int acount[100001]; 5 6 const int n = s.size(); 7 acount[n] = 0; 8 for (int i = n - 1; i >= 0; i--) { 9 acount[i] = acount[i + 1] + (s[i] == 'a'); 10 } 11 12 int acnt = 0, bcnt = 0, res = acount[0]; 13 for (int i = 0; i < n; i++) { 14 s[i] == 'a' ? acnt++ : bcnt++; 15 res = min(res, bcnt + acount[i] - 1); 16 } 17 18 return res; 19 } 20 };