1525.cpp (677B)
1 class Solution { 2 public: 3 int numSplits(const string &s) { 4 bool seen[27] = {0}; 5 int uniq[100000] = {0}; 6 7 for (int i = 0, acc = 0; i < s.size(); i++) { 8 if (!seen[s[i] & 0x1F]) { 9 seen[s[i] & 0x1F] = true; 10 acc++; 11 } 12 uniq[i] = acc; 13 } 14 15 memset(seen, 0x00, sizeof(seen)); 16 seen[s.back() & 0x1F] = true; 17 18 int res = 0; 19 for (int i = s.size() - 2, acc = 1; i >= 0; i--) { 20 if (uniq[i] == acc) res++; 21 if (seen[s[i] & 0x1F]) continue; 22 seen[s[i] & 0x1F] = true; 23 acc++; 24 } 25 26 return res; 27 } 28 };