-
Notifications
You must be signed in to change notification settings - Fork 0
/
19_License_key_formatting.cpp
63 lines (50 loc) · 1.11 KB
/
19_License_key_formatting.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//{ Driver Code Starts
/* Driver program to test above function */
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//Back-end complete function Template for C++
class Solution
{
public:
string ReFormatString(string S, int K){
string ans;
int count=K;
for(int i=S.size()-1; i>=0; i--){
char ch = S[i];
if(S[i] != '-'){
if(ch>='a' && ch<='z') ch -= 32;
ans.push_back(ch);
count--;
}
if(count==0){
count = K;
ans.push_back('-');
}
}
reverse(ans.begin(), ans.end());
if(ans[0]=='-' && ans.size()!=1){
return ans.substr(1);
}
return ans;
}
};
//{ Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
string S;
cin>>S;
int K;
cin >> K;
Solution ob;
string ans=ob.ReFormatString(S, K);
cout<<ans;
cout<<"\n";
}
return 0;
}
// } Driver Code Ends