-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrequencySort.java
More file actions
86 lines (75 loc) · 2.4 KB
/
frequencySort.java
File metadata and controls
86 lines (75 loc) · 2.4 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.*;
/**
* Author : WindAsMe
* File : frequencySort.java
* Time : Create on 18-6-15
* Location : ../Home/JavaForLeeCode2/frequencySort.java
* Function : LeeCode No.451
*/
public class frequencySort {
// Using Map is more efficient comparing with List
// First thought is sorted by Map
// But map has no obvious sort function
// private static String frequencySortResult(String s) {
// // USING TreeMap to save the frequency
// Map<Character, Integer> map = new TreeMap<>();
// for (int i = 0 ; i < s.length() ; i ++ ) {
// char temp = s.charAt(i);
// Integer value = map.get(temp);
// if ( value == null) {
// map.put(temp, 1);
// } else {
// value += 1;
// map.put(temp, value);
// }
// }
//
// }
// Second sorted by List
// Using List<char[]> to save
private static String frequencySortResult(String s) {
// USING List to save the frequency
List<char[]> list = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
char temp = s.charAt(i);
boolean mark = false;
for (int index = 0; index < list.size(); index++) {
// If in list has this word:
if (list.get(index)[0] == temp) {
char[] old = list.get(index);
char[] late = new char[2];
late[0] = temp;
late[1] = (char) (old[1] + 1);
list.remove(old);
list.add(late);
mark = true;
break;
}
}
if (!mark) {
char[] late = new char[2];
late[0] = temp;
late[1] = '1';
list.add(late);
}
}
list.sort((o1, o2) -> {
int temp = o2[1] - o1[1];
if (temp >= 0) {
return 1;
} else {
return -1;
}
});
StringBuilder result = new StringBuilder();
for (char[] aList : list) {
for (int j = 0; j < aList[1] - 48; j++) {
result.append(aList[0]);
}
}
return result.toString();
}
public static void main (String[]args){
frequencySortResult("aqwqqqqa");
}
}