diff --git a/Array/2828. Check if a String Is an Acronym of Words.py b/Array/2828. Check if a String Is an Acronym of Words.py deleted file mode 100644 index e27850c..0000000 --- a/Array/2828. Check if a String Is an Acronym of Words.py +++ /dev/null @@ -1,9 +0,0 @@ -from typing import List - - -class Solution: - def isAcronym(self, words: List[str], s: str) -> bool: - acronym = '' - for word in words: - acronym += word[0] - return acronym == s diff --git a/README.md b/README.md index a9dd50f..ac8d1d8 100644 --- a/README.md +++ b/README.md @@ -41,11 +41,11 @@ Each problem follows this structure: ## Solutions -📊 **Statistics**: 83 problems solved (migrated to new structure) +📊 **Statistics**: 84 problems solved (migrated to new structure) | Difficulty | Count | |------------|-------| -| Easy | 58 | +| Easy | 59 | | Medium | 24 | | Hard | 1 | diff --git a/docs/EASY.md b/docs/EASY.md index 09937fd..1d548ba 100644 --- a/docs/EASY.md +++ b/docs/EASY.md @@ -1,6 +1,6 @@ # Easy Problems -Total: 58 problems solved +Total: 59 problems solved ## Solutions @@ -61,6 +61,7 @@ Total: 58 problems solved | 2469 | [Convert the Temperature](https://leetcode.com/problems/convert-the-temperature/) | [solution.py](../problems/easy/convert_the_temperature_2469/solution.py) | [test_solution.py](../problems/easy/convert_the_temperature_2469/test_solution.py) | | 2710 | [Remove Trailing Zeros From a String](https://leetcode.com/problems/remove-trailing-zeros-from-a-string/) | [solution.py](../problems/easy/remove_trailing_zeros_from_a_string_2710/solution.py) | [test_solution.py](../problems/easy/remove_trailing_zeros_from_a_string_2710/test_solution.py) | | 2798 | [Number of Employees Who Met the Target](https://leetcode.com/problems/number-of-employees-who-met-the-target/) | [solution.py](../problems/easy/number_of_employees_who_met_the_target_2798/solution.py) | [test_solution.py](../problems/easy/number_of_employees_who_met_the_target_2798/test_solution.py) | +| 2828 | [Check if a String Is an Acronym of Words](https://leetcode.com/problems/check-if-a-string-is-an-acronym-of-words/) | [solution.py](../problems/easy/check_if_a_string_is_an_acronym_of_words_2828/solution.py) | [test_solution.py](../problems/easy/check_if_a_string_is_an_acronym_of_words_2828/test_solution.py) | | 2942 | [Find Words Containing Character](https://leetcode.com/problems/find-words-containing-character/) | [solution.py](../problems/easy/find_words_containing_character_2942/solution.py) | [test_solution.py](../problems/easy/find_words_containing_character_2942/test_solution.py) | | 3110 | [Score of a String](https://leetcode.com/problems/score-of-a-string/) | [solution.py](../problems/easy/score_of_a_string_3110/solution.py) | [test_solution.py](../problems/easy/score_of_a_string_3110/test_solution.py) | | 3289 | [The Two Sneaky Numbers of Digitville](https://leetcode.com/problems/the-two-sneaky-numbers-of-digitville/) | [solution.py](../problems/easy/the_two_sneaky_numbers_of_digitville_3289/solution.py) | [test_solution.py](../problems/easy/the_two_sneaky_numbers_of_digitville_3289/test_solution.py) | diff --git a/problems/easy/check_if_a_string_is_an_acronym_of_words_2828/__init__.py b/problems/easy/check_if_a_string_is_an_acronym_of_words_2828/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/problems/easy/check_if_a_string_is_an_acronym_of_words_2828/solution.py b/problems/easy/check_if_a_string_is_an_acronym_of_words_2828/solution.py new file mode 100644 index 0000000..156b3e7 --- /dev/null +++ b/problems/easy/check_if_a_string_is_an_acronym_of_words_2828/solution.py @@ -0,0 +1,23 @@ +# Tags: Array, String +from typing import List + + +class Solution: + def is_acronym(self, words: List[str], s: str) -> bool: + """ + Time complexity: O(n) + n represents the number of words in the words list + Loop through words list and build acronym: O(n) + String comparison acronym == s: Python short-circuits at first mismatch, + worst case O(min(n, m)) where m = len(s) + Since O(min(n, m)) ≤ O(n), overall time complexity is O(n) + + Space complexity: O(n) + n represents the number of words in the words list + acronym string is formed by taking first character of each word, length is n + No other significant space usage + """ + acronym = '' + for word in words: + acronym += word[0] + return acronym == s diff --git a/problems/easy/check_if_a_string_is_an_acronym_of_words_2828/test_solution.py b/problems/easy/check_if_a_string_is_an_acronym_of_words_2828/test_solution.py new file mode 100644 index 0000000..ce9b456 --- /dev/null +++ b/problems/easy/check_if_a_string_is_an_acronym_of_words_2828/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, words, s, expected, timeout", [ + ('is_acronym', ["alice", "bob", "charlie"], "abc", True, None), + ('is_acronym', ["an", "apple"], "a", False, None), + ('is_acronym', ["never", "gonna", "give", "up", "on", "you"], "ngguoy", True, None), + ]) + def test_is_acronym(self, method_name, words, s, expected, timeout): + self._run_test(self.solution, method_name, (words, s,), expected, timeout)