|
| 1 | +{ |
| 2 | + "problem_name": "implement_trie_prefix_tree", |
| 3 | + "solution_class_name": "Trie(DictTree[str])", |
| 4 | + "problem_number": "208", |
| 5 | + "problem_title": "Implement Trie (Prefix Tree)", |
| 6 | + "difficulty": "Medium", |
| 7 | + "topics": "Hash Table, String, Design, Trie", |
| 8 | + "tags": ["grind-75"], |
| 9 | + "readme_description": "A **trie** (pronounced as \"try\") or **prefix tree** is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker.\n\nImplement the Trie class:\n\n- `Trie()` Initializes the trie object.\n- `void insert(String word)` Inserts the string `word` into the trie.\n- `boolean search(String word)` Returns `true` if the string `word` is in the trie (i.e., was inserted before), and `false` otherwise.\n- `boolean startsWith(String prefix)` Returns `true` if there is a previously inserted string `word` that has the prefix `prefix`, and `false` otherwise.", |
| 10 | + "readme_examples": [ |
| 11 | + { |
| 12 | + "content": "```\nInput\n[\"Trie\", \"insert\", \"search\", \"search\", \"startsWith\", \"insert\", \"search\"]\n[[], [\"apple\"], [\"apple\"], [\"app\"], [\"app\"], [\"app\"], [\"app\"]]\nOutput\n[null, null, true, false, true, null, true]\n```\n\n**Explanation:**\n```python\ntrie = Trie()\ntrie.insert(\"apple\")\ntrie.search(\"apple\") # return True\ntrie.search(\"app\") # return False\ntrie.starts_with(\"app\") # return True\ntrie.insert(\"app\")\ntrie.search(\"app\") # return True\n```" |
| 13 | + } |
| 14 | + ], |
| 15 | + "readme_constraints": "- `1 <= word.length, prefix.length <= 2000`\n- `word` and `prefix` consist only of lowercase English letters.\n- At most `3 * 10^4` calls **in total** will be made to `insert`, `search`, and `starts_with`.", |
| 16 | + "readme_additional": "", |
| 17 | + "solution_imports": "from leetcode_py.data_structures import DictTree", |
| 18 | + "solution_methods": [ |
| 19 | + { "name": "__init__", "parameters": "", "return_type": "None", "dummy_return": "" }, |
| 20 | + { "name": "insert", "parameters": "word: str", "return_type": "None", "dummy_return": "" }, |
| 21 | + { "name": "search", "parameters": "word: str", "return_type": "bool", "dummy_return": "False" }, |
| 22 | + { |
| 23 | + "name": "starts_with", |
| 24 | + "parameters": "prefix: str", |
| 25 | + "return_type": "bool", |
| 26 | + "dummy_return": "False" |
| 27 | + } |
| 28 | + ], |
| 29 | + "test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Trie", |
| 30 | + "test_class_name": "ImplementTriePrefixTree", |
| 31 | + "test_helper_methods": [], |
| 32 | + "test_methods": [ |
| 33 | + { |
| 34 | + "name": "test_trie_operations", |
| 35 | + "parametrize": "operations, inputs, expected", |
| 36 | + "parametrize_typed": "operations: list[str], inputs: list[list[str]], expected: list[bool | None]", |
| 37 | + "test_cases": "[(['Trie', 'insert', 'search', 'search', 'starts_with', 'insert', 'search'], [[], ['apple'], ['apple'], ['app'], ['app'], ['app'], ['app']], [None, None, True, False, True, None, True]), (['Trie', 'insert', 'insert', 'search', 'search', 'starts_with', 'starts_with'], [[], ['hello'], ['world'], ['hello'], ['hi'], ['hel'], ['wor']], [None, None, None, True, False, True, True]), (['Trie', 'insert', 'insert', 'search', 'search', 'starts_with', 'starts_with'], [[], ['a'], ['aa'], ['a'], ['aa'], ['a'], ['aa']], [None, None, None, True, True, True, True]), (['Trie', 'insert', 'search', 'starts_with', 'insert', 'search', 'starts_with'], [[], ['test'], ['testing'], ['test'], ['testing'], ['testing'], ['test']], [None, None, False, True, None, True, True]), (['Trie', 'search', 'starts_with'], [[], ['empty'], ['empty']], [None, False, False])]", |
| 38 | + "body": "trie: Trie | None = None\nresults: list[bool | None] = []\nfor i, op in enumerate(operations):\n if op == 'Trie':\n trie = Trie()\n results.append(None)\n elif op == 'insert' and trie is not None:\n trie.insert(inputs[i][0])\n results.append(None)\n elif op == 'search' and trie is not None:\n results.append(trie.search(inputs[i][0]))\n elif op == 'starts_with' and trie is not None:\n results.append(trie.starts_with(inputs[i][0]))\nassert results == expected" |
| 39 | + } |
| 40 | + ], |
| 41 | + "playground_imports": "from solution import Trie", |
| 42 | + "playground_test_case": "# Example test case\noperations = ['Trie', 'insert', 'search', 'search', 'starts_with', 'insert', 'search']\ninputs = [[], ['apple'], ['apple'], ['app'], ['app'], ['app'], ['app']]\nexpected = [None, None, True, False, True, None, True]", |
| 43 | + "playground_execution": "trie = None\nresults: list[bool | None] = []\nfor i, op in enumerate(operations):\n if op == 'Trie':\n trie = Trie()\n results.append(None)\n elif op == 'insert' and trie is not None:\n trie.insert(inputs[i][0])\n results.append(None)\n elif op == 'search' and trie is not None:\n results.append(trie.search(inputs[i][0]))\n elif op == 'starts_with' and trie is not None:\n results.append(trie.starts_with(inputs[i][0]))\nresults", |
| 44 | + "playground_assertion": "assert results == expected" |
| 45 | +} |
0 commit comments