-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.java
More file actions
33 lines (27 loc) · 881 Bytes
/
solution.java
File metadata and controls
33 lines (27 loc) · 881 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
import java.util.*;
class Solution {
public String makeLargestSpecial(String s) {
List<String> parts = new ArrayList<>();
int count = 0;
int start = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '1')
count++;
else
count--;
if (count == 0) {
// Recursively process inner part
String inner = makeLargestSpecial(s.substring(start + 1, i));
parts.add("1" + inner + "0");
start = i + 1;
}
}
// Sort descending
Collections.sort(parts, Collections.reverseOrder());
StringBuilder result = new StringBuilder();
for (String p : parts) {
result.append(p);
}
return result.toString();
}
}