-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmallestString.java
More file actions
36 lines (30 loc) · 997 Bytes
/
SmallestString.java
File metadata and controls
36 lines (30 loc) · 997 Bytes
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
import java.util.Scanner;
public class SmallestString {
public static int[] computeWeights() {
int[] weights = new int[26];
weights[0] = 1;
for (int i = 1; i < 26; i++) {
weights[i] = (i + 1) * weights[i - 1] + weights[i - 1];
}
return weights;
}
public static String shortestString(int weight) {
int[] weights = computeWeights();
StringBuilder result = new StringBuilder();
for (int i = 25; i >= 0; i--) {
if (weight >= weights[i]) {
int count = weight / weights[i];
for (int j = 0; j < count; j++) {
result.append((char) ('A' + i));
}
weight %= weights[i];
}
}
return result.reverse().toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
System.out.println(shortestString(a));
}
}