2
public String repeatLimitedString(String s, int repeatLimit) {3
int[] counter = new int[26];5
for (char ch : s.toCharArray()) {7
max = Math.max(max, curr);11
StringBuilder builder = new StringBuilder();13
builder.append((char) ('a' + max));16
if (counter[max] == 0) {17
max = findNextMax(counter, max - 1);21
if (repeated == repeatLimit) {22
// Greedy, use the next possible char once and get back to curr.23
// if no other char available, the curr word is the largest subsequence.24
int lower = findNextMax(counter, max - 1);26
return builder.toString();28
builder.append((char) ('a' + lower));33
return builder.toString();36
private int findNextMax(int[] counter, int from) {39
if (counter[curr] > 0) {