← 返回 bloomberg 的题目列表Trie Data Structure
类型:online_judge
Given a string array, construct a Trie (prefix tree), and implement the following functionalities: 1. Insert a string; 2. Search if a string exists in the Trie; 3. Determine if there is any string in the Trie that starts with a certain prefix. Provide time complexity analysis for insert, delete, and search operations.
Example
trie = Trie()
trie.insert("apple")
print(trie.search("apple")) # returns True
print(trie.search("app")) # returns False
print(trie.startsWith("app")) # returns True
trie.insert("app")
print(trie.search("app")) # returns True
Data Constraints
Total length of all strings does not exceed 10^4
All strings consist of lowercase letters only
Example
Input
trie = Trie()
trie.insert("apple")
trie.search("apple")
trie.search("app")
trie.startsWith("app")
trie.insert("app")
trie.search("app")