-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIncreasingDecreasingString.java
45 lines (40 loc) · 1.26 KB
/
IncreasingDecreasingString.java
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
package com.smlnskgmail.jaman.leetcodejava.easy;
// https://leetcode.com/problems/increasing-decreasing-string/
public class IncreasingDecreasingString {
private final String input;
public IncreasingDecreasingString(String input) {
this.input = input;
}
public String solution() {
int[] freq = new int[26];
int max = 0;
for (int i = 0; i < input.length(); i++) {
int c = input.charAt(i) - 97;
int count = freq[c];
freq[c] = ++count;
if (count > max) {
max = count;
}
}
char[] result = new char[input.length()];
int pointer = 0;
while (max != 0) {
for (int i = 0; i < freq.length; i++) {
int count = freq[i];
if (count != 0) {
result[pointer++] = (char) (i + 97);
freq[i] = count - 1;
}
}
for (int i = freq.length - 1; i >= 0; i--) {
int count = freq[i];
if (count != 0) {
result[pointer++] = (char) (i + 97);
freq[i] = count - 1;
}
}
max--;
}
return String.valueOf(result);
}
}