Sort character by frequency#7391
Sort character by frequency#7391lieutenant-Rohit wants to merge 6 commits intoTheAlgorithms:masterfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7391 +/- ##
============================================
+ Coverage 79.53% 79.55% +0.02%
- Complexity 7176 7184 +8
============================================
Files 798 799 +1
Lines 23467 23486 +19
Branches 4617 4621 +4
============================================
+ Hits 18665 18685 +20
Misses 4055 4055
+ Partials 747 746 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
felseje
left a comment
There was a problem hiding this comment.
I went through the Checkstyle findings and left some comments to help guide the necessary changes.
| int diff = b.getValue() - a.getValue(); | ||
|
|
||
| // If frequency same, sort by character | ||
| if (diff == 0) return a.getKey() - b.getKey(); |
There was a problem hiding this comment.
Instead of using a single-line if statement like:
if (diff == 0) return a.getKey() - b.getKey();
please use braces:
if (diff == 0) {
return a.getKey() - b.getKey();
}
| @@ -0,0 +1,66 @@ | |||
| // Reference: https://leetcode.com/problems/sort-characters-by-frequency/ | |||
| package com.thealgorithms.strings; | |||
| import java.util.*; | |||
There was a problem hiding this comment.
Avoid using wildcard imports (e.g., java.util.*). Please import only the specific classes you need.
| @@ -0,0 +1,49 @@ | |||
| package com.thealgorithms.strings; | |||
|
|
|||
| import static org.junit.jupiter.api.Assertions.*; | |||
There was a problem hiding this comment.
Avoid using wildcard imports (e.g., org.junit.jupiter.api.Assertions.*). Please import only the specific classes you need.
felseje
left a comment
There was a problem hiding this comment.
Hello, @lieutenant-Rohit ! How’s it going?
I’ve left a code review based on the compilation errors from the automated build. Hope the comments help!
| public String frequencySort(String s) { | ||
|
|
||
| // Step 1: Count frequency of each character | ||
| Map<Character, Integer> map = new HashMap<>(); |
There was a problem hiding this comment.
To use the Map and HashMap classes, you will need to import them:
import java.util.Map;
import java.util.HashMap;
| } | ||
|
|
||
| // Step 2: Create max-heap based on frequency | ||
| PriorityQueue<Map.Entry<Character, Integer>> pq = new PriorityQueue<>((a, b) -> { |
There was a problem hiding this comment.
To use the PriorityQueue class, you will need to import it:
import java.util.PriorityQueue;
|
|
||
| private final SortCharacterByFrequency solution = new SortCharacterByFrequency(); | ||
|
|
||
| @Test |
There was a problem hiding this comment.
To use @Test annotation, you will need to import it:
import org.junit.jupiter.api.Test;
| String result = solution.frequencySort("tree"); | ||
|
|
||
| // Possible outputs: "eert" or "eetr" | ||
| assertTrue(result.equals("eert") || result.equals("eetr")); |
There was a problem hiding this comment.
To use assertTrue() method, you will need to import it:
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||
| @Test | ||
| void testSingleCharacter() { | ||
| assertEquals("a", solution.frequencySort("a")); |
There was a problem hiding this comment.
To use assertEquals() method, you will need to import it:
import static org.junit.jupiter.api.Assertions.assertEquals;
|
This pull request has been automatically closed because its workflows or checks failed and it has been inactive for more than 14 days. Please fix the workflows and reopen if you'd like to continue. Merging from main/master alone does not count as activity. |
Added SortCharacterByFrequency algorithm using HashMap and Priority Queue