-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathSolution.java
More file actions
69 lines (59 loc) · 2.05 KB
/
Solution.java
File metadata and controls
69 lines (59 loc) · 2.05 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
scanner.nextLine(); // Consume newline
for (int t = 1; t <= T; t++) {
String input = scanner.nextLine();
String output = getSmallestString(input);
System.out.println("Case #" + t + ": " + output);
}
}
private static String getSmallestString(String input) {
List<Pair<Character, Integer>> groupedInput = groupCharacters(input);
StringBuilder result = new StringBuilder();
for (int i = 0; i < groupedInput.size(); i++) {
Pair<Character, Integer> pair = groupedInput.get(i);
char current = pair.key;
int count = pair.value;
if (i < groupedInput.size() - 1 && current < groupedInput.get(i + 1).key) {
for (int j = 0; j < 2 * count; j++) {
result.append(current);
}
} else {
for (int j = 0; j < count; j++) {
result.append(current);
}
}
}
return result.toString();
}
private static List<Pair<Character, Integer>> groupCharacters(String input) {
List<Pair<Character, Integer>> groups = new ArrayList<>();
char currentChar = input.charAt(0);
int count = 1;
for (int i = 1; i < input.length(); i++) {
char c = input.charAt(i);
if (c == currentChar) {
count++;
} else {
groups.add(new Pair<>(currentChar, count));
currentChar = c;
count = 1;
}
}
groups.add(new Pair<>(currentChar, count)); // Add the last group
return groups;
}
private static class Pair<K, V> {
K key;
V value;
Pair(K key, V value) {
this.key = key;
this.value = value;
}
}
}