0482.cpp (438B)
1 class Solution { 2 public: 3 string licenseKeyFormatting(const string &s, int k) const { 4 const int n = std::count(begin(s), end(s), '-'); 5 const int m = size(s) - n; 6 7 string res; 8 int goal = m % k ? m % k + 1 : k + 1; 9 for (const char c : s) { 10 if (c == '-') continue; 11 if (!--goal) res += '-', goal = k; 12 13 res += toupper(c); 14 } 15 16 return res; 17 } 18 };