diff --git a/Hash Table/1941. Check if All Characters Have Equal Number of Occurrences.py b/Hash Table/1941. Check if All Characters Have Equal Number of Occurrences.py deleted file mode 100644 index 5b95f92..0000000 --- a/Hash Table/1941. Check if All Characters Have Equal Number of Occurrences.py +++ /dev/null @@ -1,9 +0,0 @@ -from collections import defaultdict - - -class Solution: - def areOccurrencesEqual(self, s: str) -> bool: - count = defaultdict(int) - for c in s: - count[c] += 1 - return len(set(count.values())) == 1 diff --git a/problems/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941/__init__.py b/problems/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/problems/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941/solution.py b/problems/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941/solution.py new file mode 100644 index 0000000..b06ad87 --- /dev/null +++ b/problems/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941/solution.py @@ -0,0 +1,23 @@ +# Tags: String, Hash Table +from collections import Counter + + +class Solution: + def are_occurrences_equal(self, s: str) -> bool: + """ + Time complexity: O(n) + - n represents the length of the input string s + - Counter(s) traverses the entire string once: O(n) + - count_table.values() extracts frequency values: O(k), k = unique chars + - set() removes duplicates: O(k) + - len() calculates set size: O(1) + - Since k <= n, overall: O(n) + + Space complexity: O(k) + - k represents the number of unique characters + - Counter stores k character-frequency pairs: O(k) + - The set of unique frequencies: O(k) + - Overall: O(k), where k <= n + """ + count_table = Counter(s) + return len(set(count_table.values())) == 1 diff --git a/problems/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941/test_solution.py b/problems/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941/test_solution.py new file mode 100644 index 0000000..1323715 --- /dev/null +++ b/problems/easy/CheckIfAllCharactersHaveEqualNumberOfOccurrences1941/test_solution.py @@ -0,0 +1,14 @@ +import pytest +from tests.base_test import BaseTestSolution +from .solution import Solution + + +class TestSolution(BaseTestSolution): + solution = Solution() + + @pytest.mark.parametrize("method_name, s, expected, timeout", [ + ('are_occurrences_equal', "abacbc", True, None), + ('are_occurrences_equal', "aaabb", False, None), + ]) + def test_are_occurrences_equal(self, method_name, s, expected, timeout): + self._run_test(self.solution, method_name, (s,), expected, timeout)