Leetcode 482 Solution

This article provides solution to leetcode question 482 (license-key-formatting)

https://leetcode.com/problems/license-key-formatting

Solution

class Solution { public: string licenseKeyFormatting(string S, int K) { string t; int cnt = 0;
for (int i = S.size() - 1; i >= 0; i--) { if (S[i] == '-') continue;
if (cnt != 0 && cnt % K == 0) t += "-";
t += toupper(S[i]);
cnt++; }
reverse(t.begin(), t.end());
return t; } };