2645.cpp (361B)
1 class Solution { 2 public: 3 int addMinimum(const string &word) const { 4 static const int add[3][3] = {{2, 1, 0}, {0, 2, 1}, {1, 0, 2}}; 5 int res = 0, prev = 2; 6 7 for (const char c : word) { 8 const int idx = c - 'a'; 9 res += add[idx][prev]; 10 prev = idx; 11 } 12 13 return res + 2 - prev; 14 } 15 };