方法一:
1、小写变大写
2、插入“-”
class Solution {
public:
string licenseKeyFormatting(string S, int K) {
int cnt = 0;
int len = S.size();
string temp = "";
for(int i = len - 1; i >= 0; i--){ // 第一步:将小写字母转化为大写字母,并且去掉'-'
if(S[i] >= 'a' && S[i] <= 'z'){
S[i] -= 32;
}
if(S[i] == '-'){
continue;
}
temp += S[i];
}
string res = "";
len = temp.size();
for(int i = 0; i < len; i++){// 第二步:插入 '-'
if(cnt == K){
cnt = 0;
res += '-';
}
cnt++;
res += temp[i];
}
reverse(res.begin(),res.end()); // 反转
return res;
}
};