-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalanced string.java
More file actions
55 lines (43 loc) · 1.36 KB
/
Balanced string.java
File metadata and controls
55 lines (43 loc) · 1.36 KB
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
//{ Driver Code Starts
// Initial Template for Java
import java.io.*;
import java.util.*;
class GFG {
// Position this line where user code will be pasted.
public static void main(String args[]) throws IOException {
BufferedReader read =
new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
int N = Integer.parseInt(read.readLine());
Solution ob = new Solution();
System.out.println(ob.BalancedString(N));
}
}
}
// } Driver Code Ends
// User function Template for Java
class Solution {
private static final String ALPHABETS = "abcdefghijklmnopqrstuvwxyz";
static String BalancedString(int N) {
StringBuilder res = new StringBuilder(N);
res.append(ALPHABETS.repeat(N / 26));
int sod = sumOfDigits(N);
N %= 26;
if(N == 0) {
return res.toString();
}
int count = ((N >> 1) + (N & 1) * (1 - (sod & 1)));
res.append(ALPHABETS, 0, count);
res.append(ALPHABETS, 26 - N + count, 26);
return res.toString();
}
private static int sumOfDigits(int n) {
int sum = 0;
while(n != 0) {
sum += n%10;
n /= 10;
}
return sum;
}
}