-
Notifications
You must be signed in to change notification settings - Fork 0
/
Balanced string.cpp
72 lines (55 loc) · 1.48 KB
/
Balanced string.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
64
65
66
67
68
69
70
71
72
//{ Driver Code Starts
// Initial template for c++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function template for c++
class Solution {
public:
string BalancedString(int N) {
int times = N / 26;
int nums = N % 26;
string temp = "abcdefghijklmnopqrstuvwxyz";
string ans = "";
while(times--) {
ans += temp;
}
if(nums % 2 == 0) {
// cout<<"even"<<endl;
ans += temp.substr(0, nums >> 1);
ans += temp.substr(26 - (nums >> 1));
}
else {
int sum = 0;
int n = N;
while(n) {
sum += n%10;
n /= 10;
}
if(sum % 2 == 0) {
// cout<<"odd even"<<endl;
ans += temp.substr(0, (nums + 1) >> 1);
ans += temp.substr(26 - ((nums - 1) >> 1));
}
else {
// cout<<"odd odd"<<endl;
ans += temp.substr(0, (nums - 1) >> 1);
ans += temp.substr(26 - ((nums + 1) >> 1));
}
}
return ans;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int N;
cin >> N;
Solution ob;
cout << ob.BalancedString(N) << endl;
}
return 0;
}
// } Driver Code Ends