Skip to content

Commit 46bf9e2

Browse files
Add SortCharacterByFrequency implementation with unit tests
1 parent 951ac54 commit 46bf9e2

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Reference: https://leetcode.com/problems/sort-characters-by-frequency/
2+
package com.thealgorithms.strings;
23
import java.util.*;
34

5+
46
// Problem: Sort Characters By Frequency
57
// Pattern: HashMap + Priority Queue (Heap)
68
// Difficulty: Medium
@@ -16,7 +18,7 @@
1618
Space Complexity: O(n)
1719
*/
1820

19-
class Solution {
21+
class SortCharacterByFrequency {
2022
public String frequencySort(String s) {
2123

2224
// Step 1: Count frequency of each character

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,12 @@ void testMixedCharacters() {
3838
// Possible outputs: "cccaaa" or "aaaccc"
3939
assertTrue(result.equals("cccaaa") || result.equals("aaaccc"));
4040
}
41+
42+
@Test
43+
void testWithNumbersAndLetters() {
44+
String result = solution.frequencySort("Aabb");
45+
46+
// 'b' appears twice → should come first
47+
assertTrue(result.startsWith("bb"));
48+
}
4149
}

0 commit comments

Comments
 (0)