-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsort_characters_by_frequency.dart
143 lines (114 loc) · 3.64 KB
/
sort_characters_by_frequency.dart
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
-* 451. Sort Characters By Frequency *-
Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string.
Return the sorted string. If there are multiple answers, return any of them.
Example 1:
Input: s = "tree"
Output: "eert"
Explanation: 'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
Example 2:
Input: s = "cccaaa"
Output: "aaaccc"
Explanation: Both 'c' and 'a' appear three times, so both "cccaaa" and "aaaccc" are valid answers.
Note that "cacaca" is incorrect, as the same characters must be together.
Example 3:
Input: s = "Aabb"
Output: "bbAa"
Explanation: "bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters.
Constraints:
1 <= s.length <= 5 * 105
s consists of uppercase and lowercase English letters and digits.
*/
// class Pair {
// late int val;
// late String c;
// Pair(String c, int val) {
// this.c = c;
// this.val = val;
// }
// }
// class A {
// String frequencySort(String s) {
// // PriorityQueue for sorting elements on decreasing order based on frequency
// // b.val - a.val => store pair objects based on decreasing order of frequency value
// Queue<Pair> pq = Queue()..toList().sort((a, b) => b.val - a.val);
// // HashMap to store elements and their current frequency
// HashMap<String, int> hm = HashMap();
// List<String> c = s.split("");
// for (int i = 0; i < c.length; i++) {
// int count = hm[c[i]] ?? 0;
// //add element and current frequency in hashmap
// hm[c[i]] = count + 1;
// }
// //add whole hashmap to priorityQueue
// for (MapEntry<String, int> entry in hm.entries) {
// Pair pair = new Pair(entry.key, entry.value);
// pq.add(pair);
// }
// StringBuffer sb = StringBuffer("");
// //extract value from priorityQueue and add it into answer string
// while (pq.isNotEmpty) {
// Pair pair = pq.removeFirst();
// int count = pair.val;
// String ch = pair.c;
// // add character into answer string as many time as frequency (it appears in main string)
// while (count-- > 0) {
// sb.write(ch);
// }
// }
// return sb.toString();
// }
// }
import 'dart:collection';
class Pair {
late final String c;
late final int val;
Pair(int val, String c) {
this.c = c;
this.val = val;
}
}
class B {
String frequencySort(String s) {
HashMap<String, int> map = HashMap();
for (String c in s.split("")) {
map[c] = (map[c] ?? 0) + 1;
}
//heap
Queue<String> pq = Queue()
..toList().sort((a, b) => (map[b]! - map[a]!)); //for decreasing order
pq.addAll(map.keys);
//putting in string builder
StringBuffer sb = StringBuffer();
while (!pq.isEmpty) {
String c = pq.removeFirst();
for (int i = 0; i < map[c]!; i++) {
sb.write(c);
}
}
return sb.toString();
}
}
class C {
String frequencySort(String s) {
HashMap<String, int> map = HashMap();
for (int i = 0; i < s.length; i++) {
map[s[i]] = (map[s[i]] ?? 0) + 1;
}
// convert map into the list for comparison
List<MapEntry<String, int>> list = map.entries.toList();
// sorting the list for the elements
list.sort((a, b) => b.value.compareTo(a.value));
StringBuffer sb = StringBuffer();
for (MapEntry<String, int> e in list) {
int i = e.value;
while (i > 0) {
sb.write(e.key);
i--;
}
}
return sb.toString();
}
}