License Key Formatting LeetCode solution in C++

1
class Solution {
2
public:
3
string licenseKeyFormatting(string S, int K) {
4
stack<char> sChars;
5
for (auto ch : S) {
6
if (ch != '-') sChars.push(ch);
7
}
8

9
string sRet;
10
int cur = 0;
11
while (!sChars.empty()) {
12
sRet += toupper(sChars.top());
13
sChars.pop();
14
cur++;
15

16
if (cur == K && !sChars.empty()) {
17
sRet += '-';
18
cur = 0;
19
}
20
}
21
reverse(sRet.begin(), sRet.end());
22
return sRet;
23
}
24
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0