Skip to content

Commit 951ac54

Browse files
Format SortCharacterByFrequency and tests using clang-format
1 parent 495eec5 commit 951ac54

2 files changed

Lines changed: 9 additions & 10 deletions

File tree

src/main/java/com/thealgorithms/strings/SortCharacterByFrequency.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,15 @@ public String frequencySort(String s) {
3030
}
3131

3232
// Step 2: Create max-heap based on frequency
33-
PriorityQueue<Map.Entry<Character, Integer>> pq =
34-
new PriorityQueue<>((a, b) -> {
33+
PriorityQueue<Map.Entry<Character, Integer>> pq = new PriorityQueue<>((a, b) -> {
34+
// Higher frequency comes first
35+
int diff = b.getValue() - a.getValue();
3536

36-
// Higher frequency comes first
37-
int diff = b.getValue() - a.getValue();
37+
// If frequency same, sort by character
38+
if (diff == 0) return a.getKey() - b.getKey();
3839

39-
// If frequency same, sort by character
40-
if (diff == 0) return a.getKey() - b.getKey();
41-
42-
return diff;
43-
});
40+
return diff;
41+
});
4442

4543
// Add all entries to heap
4644
pq.addAll(map.entrySet());
@@ -52,7 +50,7 @@ public String frequencySort(String s) {
5250

5351
Map.Entry<Character, Integer> entry = pq.poll();
5452

55-
char key = entry.getKey(); // character
53+
char key = entry.getKey(); // character
5654
int freq = entry.getValue(); // frequency
5755

5856
// Append character 'freq' times

src/test/java/com/thealgorithms/strings/SortCharacterByFrequencyTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.strings;
22

33
import static org.junit.jupiter.api.Assertions.*;
4+
45
import org.junit.jupiter.api.Test;
56

67
class SortCharacterByFrequencyTest {

0 commit comments

Comments
 (0)