diff --git a/README.md b/README.md index 076b807..f2bf251 100644 --- a/README.md +++ b/README.md @@ -41,11 +41,11 @@ Each problem follows this structure: ## Solutions -📊 **Statistics**: 84 problems solved (migrated to new structure) +📊 **Statistics**: 85 problems solved (migrated to new structure) | Difficulty | Count | |------------|-------| -| Easy | 59 | +| Easy | 60 | | Medium | 24 | | Hard | 1 | diff --git a/String/1684. Count the Number of Consistent Strings.py b/String/1684. Count the Number of Consistent Strings.py deleted file mode 100644 index 849b350..0000000 --- a/String/1684. Count the Number of Consistent Strings.py +++ /dev/null @@ -1,16 +0,0 @@ -from typing import List - - -class Solution: - def countConsistentStrings(self, allowed: str, words: List[str]) -> int: - set_chars = set(allowed) - res = 0 - for word in words: - is_consistent = True - for c in word: - if c not in set_chars: - is_consistent = False - break - if is_consistent: - res += 1 - return res diff --git a/docs/EASY.md b/docs/EASY.md index 1d548ba..c355f51 100644 --- a/docs/EASY.md +++ b/docs/EASY.md @@ -1,6 +1,6 @@ # Easy Problems -Total: 59 problems solved +Total: 60 problems solved ## Solutions @@ -46,6 +46,7 @@ Total: 59 problems solved | 1603 | [Design Parking System](https://leetcode.com/problems/design-parking-system/) | [solution.py](../problems/easy/design_parking_system_1603/solution.py) | [test_solution.py](../problems/easy/design_parking_system_1603/test_solution.py) | | 1662 | [Check If Two String Arrays Are Equivalent](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/) | [solution.py](../problems/easy/check_if_two_string_arrays_are_equivalent_1662/solution.py) | [test_solution.py](../problems/easy/check_if_two_string_arrays_are_equivalent_1662/test_solution.py) | | 1672 | [Richest Customer Wealth](https://leetcode.com/problems/richest-customer-wealth/) | [solution.py](../problems/easy/richest_customer_wealth_1672/solution.py) | [test_solution.py](../problems/easy/richest_customer_wealth_1672/test_solution.py) | +| 1684 | [Count the Number of Consistent Strings](https://leetcode.com/problems/count-the-number-of-consistent-strings/) | [solution.py](../problems/easy/count_the_number_of_consistent_strings_1684/solution.py) | [test_solution.py](../problems/easy/count_the_number_of_consistent_strings_1684/test_solution.py) | | 1773 | [Count Items Matching a Rule](https://leetcode.com/problems/count-items-matching-a-rule/) | [solution.py](../problems/easy/count_items_matching_a_rule_1773/solution.py) | [test_solution.py](../problems/easy/count_items_matching_a_rule_1773/test_solution.py) | | 1816 | [Truncate Sentence](https://leetcode.com/problems/truncate-sentence/) | [solution.py](../problems/easy/truncate_sentence_1816/solution.py) | [test_solution.py](../problems/easy/truncate_sentence_1816/test_solution.py) | | 1929 | [Concatenation of Array](https://leetcode.com/problems/concatenation-of-array/) | [solution.py](../problems/easy/concatenation_of_array_1929/solution.py) | [test_solution.py](../problems/easy/concatenation_of_array_1929/test_solution.py) | diff --git a/problems/easy/count_the_number_of_consistent_strings_1684/__init__.py b/problems/easy/count_the_number_of_consistent_strings_1684/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/problems/easy/count_the_number_of_consistent_strings_1684/solution.py b/problems/easy/count_the_number_of_consistent_strings_1684/solution.py new file mode 100644 index 0000000..4bc4169 --- /dev/null +++ b/problems/easy/count_the_number_of_consistent_strings_1684/solution.py @@ -0,0 +1,31 @@ +# Tags: Array, String, Hash Table +from typing import List + + +class Solution: + def count_consistent_strings(self, allowed: str, words: List[str]) -> int: + """ + Let A be the length of allowed string, and L be the total number of characters across all words. + + Time complexity: O(A + L) + - Creating the set from allowed: O(A) + - Iterating through all words and characters: O(L) total + - Each set lookup (c not in allowed_set) is O(1) on average + - Overall: O(A) + O(L) = O(A + L) + + Space complexity: O(A) + - The allowed_set set stores at most A unique characters from allowed + - Other variables (res, is_consistent) use O(1) constant space + - Overall: O(A) + """ + allowed_set = set(allowed) + res = 0 + for word in words: + is_consistent = True + for c in word: + if c not in allowed_set: + is_consistent = False + break + if is_consistent: + res += 1 + return res diff --git a/problems/easy/count_the_number_of_consistent_strings_1684/test_solution.py b/problems/easy/count_the_number_of_consistent_strings_1684/test_solution.py new file mode 100644 index 0000000..120a45c --- /dev/null +++ b/problems/easy/count_the_number_of_consistent_strings_1684/test_solution.py @@ -0,0 +1,15 @@ +import pytest +from tests.base_test import BaseTestSolution +from .solution import Solution + + +class TestSolution(BaseTestSolution): + solution = Solution() + + @pytest.mark.parametrize("method_name, allowed, words, expected, timeout", [ + ('count_consistent_strings', "ab", ["ad", "bd", "aaab", "baa", "badab"], 2, None), + ('count_consistent_strings', "abc", ["a", "b", "c", "ab", "ac", "bc", "abc"], 7, None), + ('count_consistent_strings', "cad", ["cc", "acd", "b", "ba", "bac", "bad", "ac", "d"], 4, None), + ]) + def test_count_consistent_strings(self, method_name, allowed, words, expected, timeout): + self._run_test(self.solution, method_name, (allowed, words,), expected, timeout)