From c5777d0191b70943a71bfa04488dda21b4c5a280 Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Sun, 5 Apr 2026 09:39:56 +0800 Subject: [PATCH] test: add LeetCode 1941 with tests, complexity docs, and migration - Add CheckIfAllCharactersHaveEqualNumberOfOccurrences1941 solution with HashMap/HashSet approach - Include comprehensive Javadoc with time/space complexity analysis - Add JUnit 5 test cases covering both LeetCode examples (abacbc, aaabb) - Migrate from old structure to new package structure (com.leetcode.easy) - Update project documentation: README.md and EASY.md - Increment statistics: 54 total problems (38 easy, 15 medium, 1 hard) --- README.md | 4 +-- docs/EASY.md | 3 +- ...ctersHaveEqualNumberOfOccurrences1941.java | 16 ---------- ...ctersHaveEqualNumberOfOccurrences1941.java | 32 +++++++++++++++++++ ...sHaveEqualNumberOfOccurrences1941Test.java | 23 +++++++++++++ 5 files changed, 59 insertions(+), 19 deletions(-) delete mode 100644 src/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941.java create mode 100644 src/main/java/com/leetcode/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941.java create mode 100644 src/test/java/com/leetcode/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941Test.java diff --git a/README.md b/README.md index eab1020..2acd909 100644 --- a/README.md +++ b/README.md @@ -44,11 +44,11 @@ Each problem follows this structure: ## Solutions -📊 **Statistics**: 53 problems solved (migrated to new structure) +📊 **Statistics**: 54 problems solved (migrated to new structure) | Difficulty | Count | |------------|-------| -| Easy | 37 | +| Easy | 38 | | Medium | 15 | | Hard | 1 | diff --git a/docs/EASY.md b/docs/EASY.md index 772b7b6..683f774 100644 --- a/docs/EASY.md +++ b/docs/EASY.md @@ -1,6 +1,6 @@ # Easy Problems -Total: 37 problems solved +Total: 38 problems solved ## Solutions @@ -34,6 +34,7 @@ Total: 37 problems solved | 1672 | [Richest Customer Wealth](https://leetcode.com/problems/richest-customer-wealth/) | [RichestCustomerWealth1672.java](../src/main/java/com/leetcode/easy/RichestCustomerWealth1672.java) | [RichestCustomerWealth1672Test.java](../src/test/java/com/leetcode/easy/RichestCustomerWealth1672Test.java) | | 1773 | [Count Items Matching a Rule](https://leetcode.com/problems/count-items-matching-a-rule/) | [CountItemsMatchingARule1773.java](../src/main/java/com/leetcode/easy/CountItemsMatchingARule1773.java) | [CountItemsMatchingARule1773Test.java](../src/test/java/com/leetcode/easy/CountItemsMatchingARule1773Test.java) | | 1816 | [Truncate Sentence](https://leetcode.com/problems/truncate-sentence/) | [TruncateSentence1816.java](../src/main/java/com/leetcode/easy/TruncateSentence1816.java) | [TruncateSentence1816Test.java](../src/test/java/com/leetcode/easy/TruncateSentence1816Test.java) | +| 1941 | [Check if All Characters Have Equal Number of Occurrences](https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/) | [CheckIfAllCharactersHaveEqualNumberOfOccurrences1941.java](../src/main/java/com/leetcode/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941.java) | [CheckIfAllCharactersHaveEqualNumberOfOccurrences1941Test.java](../src/test/java/com/leetcode/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941Test.java) | | 2011 | [Final Value of Variable After Performing Operations](https://leetcode.com/problems/final-value-of-variable-after-performing-operations/) | [FinalValueOfVariableAfterPerformingOperations2011.java](../src/main/java/com/leetcode/easy/FinalValueOfVariableAfterPerformingOperations2011.java) | [FinalValueOfVariableAfterPerformingOperations2011Test.java](../src/test/java/com/leetcode/easy/FinalValueOfVariableAfterPerformingOperations2011Test.java) | | 2032 | [Two Out of Three](https://leetcode.com/problems/two-out-of-three/) | [TwoOutOfThree2032.java](../src/main/java/com/leetcode/easy/TwoOutOfThree2032.java) | [TwoOutOfThree2032Test.java](../src/test/java/com/leetcode/easy/TwoOutOfThree2032Test.java) | | 2124 | [Check if All A's Appears Before All B's](https://leetcode.com/problems/check-if-all-as-appears-before-all-bs/) | [CheckIfAllAsAppearsBeforeAllBs2124.java](../src/main/java/com/leetcode/easy/CheckIfAllAsAppearsBeforeAllBs2124.java) | [CheckIfAllAsAppearsBeforeAllBs2124Test.java](../src/test/java/com/leetcode/easy/CheckIfAllAsAppearsBeforeAllBs2124Test.java) | diff --git a/src/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941.java b/src/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941.java deleted file mode 100644 index 4334839..0000000 --- a/src/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941.java +++ /dev/null @@ -1,16 +0,0 @@ -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -public class CheckIfAllCharactersHaveEqualNumberOfOccurrences1941 { - public boolean areOccurrencesEqual(String s) { - char[] chars = s.toCharArray(); - Map count = new HashMap<>(); - for (char c : chars) { - count.put(c, count.getOrDefault(c, 0) + 1); - } - Set hashSet = new HashSet<>(count.values()); - return hashSet.size() == 1; - } -} diff --git a/src/main/java/com/leetcode/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941.java b/src/main/java/com/leetcode/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941.java new file mode 100644 index 0000000..9de39b3 --- /dev/null +++ b/src/main/java/com/leetcode/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941.java @@ -0,0 +1,32 @@ +// Tags: String, Hash Table +package com.leetcode.easy; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; + +public class CheckIfAllCharactersHaveEqualNumberOfOccurrences1941 { + /** + * Checks if all characters in the string have equal number of occurrences. + * + *

Approach: + *

    + *
  • Use HashMap to count frequency of each character
  • + *
  • Put all frequency values into HashSet
  • + *
  • If HashSet size is 1, all characters have same occurrence count
  • + *
+ * + *

Time Complexity: O(n) where n is the length of the string, single pass required + *

Space Complexity: O(1) as at most 26 unique characters are stored (constraint: lowercase letters only) + * + * @param s input string containing only lowercase English letters + * @return true if all characters appear the same number of times, false otherwise + */ + public boolean areOccurrencesEqual(String s) { + Map freq = new HashMap<>(); + for (char c : s.toCharArray()) { + freq.put(c, freq.getOrDefault(c, 0) + 1); + } + return new HashSet<>(freq.values()).size() == 1; + } +} diff --git a/src/test/java/com/leetcode/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941Test.java b/src/test/java/com/leetcode/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941Test.java new file mode 100644 index 0000000..5b0ec35 --- /dev/null +++ b/src/test/java/com/leetcode/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941Test.java @@ -0,0 +1,23 @@ +package com.leetcode.easy; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +class CheckIfAllCharactersHaveEqualNumberOfOccurrences1941Test { + + @Test + void testAreOccurrencesEqualExample1() { + CheckIfAllCharactersHaveEqualNumberOfOccurrences1941 solution = new CheckIfAllCharactersHaveEqualNumberOfOccurrences1941(); + String s = "abacbc"; + boolean result = solution.areOccurrencesEqual(s); + assertTrue(result, "The characters 'a', 'b', and 'c' should all occur 2 times in s"); + } + + @Test + void testAreOccurrencesEqualExample2() { + CheckIfAllCharactersHaveEqualNumberOfOccurrences1941 solution = new CheckIfAllCharactersHaveEqualNumberOfOccurrences1941(); + String s = "aaabb"; + boolean result = solution.areOccurrencesEqual(s); + assertFalse(result, "'a' occurs 3 times while 'b' occurs 2 times, which is not equal"); + } +}