-
📚 Problem description
Implement a trie with insert, search, and startsWith methods.
Example:
Trie trie = new Trie();
trie.insert("apple");
trie.search("apple"); // returns true
trie.search("app"); // returns false
trie.startsWith("app"); // returns true
trie.insert("app");
trie.search("app"); // returns trueNote:
-
You may assume that all inputs are consist of lowercase letters a-z.
-
All inputs are guaranteed to be non-empty strings.
🎯 Solution
🧐 Approach
Trie is one of efficient data structure when dealing with string.
There are two different way to represent trie. One is using array, and the other is using hash map.
Using array is more efficient rather than hash map. However, for flexibility, I chose hash map.
📖 What I learned
I implemented trie from scratch!
'Algo Rhythm🕺💃 > LeetCode' 카테고리의 다른 글
[Algo Rhythm🕺💃] LeetCode 720. Longest Word in Dictionary (0) 2020.09.04 [Algo Rhythm🕺💃] LeetCode 653. Two Sum IV - Input is a BST (0) 2020.08.24 [Algo Rhythm🕺💃] LeetCode 167. Two Sum II - Input array is sorted (0) 2020.08.24 [Algo Rhythm🕺💃] LeetCode 78. Subsets (0) 2020.08.22 [Algo Rhythm🕺💃] LeetCode 90. SubsetsII (0) 2020.08.22 -