-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc_0482.cpp
More file actions
30 lines (30 loc) · 773 Bytes
/
lc_0482.cpp
File metadata and controls
30 lines (30 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* @file lc_0482.cpp
* @brief https://leetcode-cn.com/problems/license-key-formatting/
* @author YongDu
* @date 2021-10-04
*/
class Solution {
public:
string licenseKeyFormatting(string s, int k) {
if (s.empty()) {
return string();
}
string result;
int cnt = 0;
for (int i = s.size() - 1; i >= 0; --i) {
if (s[i] != '-') {
result.push_back(std::toupper(s[i]));
cnt++;
if (cnt % k == 0) {
result.push_back('-');
}
}
}
if (result.back() == '-') {
result.pop_back();
}
std::reverse(result.begin(), result.end());
return result;
}
};