-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBalanced string.cpp
35 lines (35 loc) · 1.2 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
class Solution {
public:
string BalancedString(int N) {
string ans = "", full = "";
for (char ch = 'a'; ch <= 'z'; ch++) full += ch;
int a = N / 26, b = N % 26, i;
while (a--) // appending a-z to answer as many times as possible
ans += full;
// when N is even
if (N % 2 == 0) {
for (i = 0; i < b / 2; i++) ans += (char)('a' + i);
for (i = 26 - b / 2; i < 26; i++) ans += (char)('a' + i);
}
// when N is odd
else {
int SumOfdigits = 0, temp = N;
// calculating sum of digits of N
while (temp) {
SumOfdigits += temp % 10;
temp /= 10;
}
// when sum of digits is even
if (SumOfdigits % 2 == 0) {
for (i = 0; i < (b + 1) / 2; i++) ans += (char)('a' + i);
for (i = 26 - b / 2; i < 26; i++) ans += (char)('a' + i);
}
// when sum of digits is odd
else {
for (i = 0; i < b / 2; i++) ans += (char)('a' + i);
for (i = 26 - (b + 1) / 2; i < 26; i++) ans += (char)('a' + i);
}
}
return ans;
}
};