Introduction Video: https://www.youtube.com/watch?v=CX777rfuZtM
Problem: find a data structure for storing strings that allows for fast insertion, search, and deletion
Trie is usually used to solve string prefix problems. Insert and find O(n) time complexity.
In a lot of problems, a trie
can be represented by a nested dict
.
1 | 'dad' |
Standard Trie
1 | class TrieNode { |
KMP Substring Search
Video: https://www.youtube.com/watch?v=GTJr8OvyEVQ
Good explanation: https://www.geeksforgeeks.org/kmp-algorithm-for-pattern-searching/
Brute Force: Time Complexity: O(mn)m
: length of textn
: length of the pattern
KMP: Time Complexity: O(m + n)
Space Comple: O(n)
1 | Text: "a b x a b c a b c a b y" |
Unlike Naive algorithm, where we slide the pattern by one and compare all characters at each shift, we use a value from lps[] to decide the next characters to be matched. The idea is to not match a character that we know will anyway match.
1 | # Construct prefix and suffix array |
214. Shortest Palindrome
Leetcode: https://leetcode.com/problems/shortest-palindrome/
Solution 1 (Brute Force):
Find the longest palindrome substring starting at index 0.
Time Complexity: O(n ^ 2)
1 | def shortestPalindrome(self, s: str) -> str: |
Solution 2: KMP
- Match
s
with reverseds
to find the longest palindrome substring starts from index 0 - Construct let
full
=s + '#' + s[::-1]
- Construct a KMP array of
full
, KMP[-1] is the length of the longest palindrome substring - Append reversed rest of string in front of
s
1 | def shortestPalindrome(self, s: str) -> str: |
336. Palindrome Pairs
Leetcode: https://leetcode.com/problems/palindrome-pairs/
[Solution]
- For each word, start from their first character as prefix, and the rest of the characters as suffix, check if the reverse is in the dictionary, if so, append the reverse to the head of the prefix.
1
2
3
4
5
6"abcd"
pref = ""
looking for "dcba"
pref = "a"
looking for "dcb"
...
1 | class Solution(object): |
211. Add and Search Word - Data structure design
Leetcode: https://leetcode.com/problems/add-and-search-word-data-structure-design/
[Solution]
Add word: use a nested dictionary to represent the Trie structure. Use
#
to represent the end of the word1
2'dad'
{d: {a: {d: {'#': '#'}}}}Search word: use recursive solution to search word
1 | class WordDictionary: |
Take Away
- To make original function recursivable with more arguments, we can set default value to the arguments.
trie = None
here.
642. Design Search Autocomplete System
Leetcode: https://leetcode.com/problems/design-search-autocomplete-system
1 | # Input: <str> end with '#'(# means sentence ends) |
208. Implement Trie (Prefix Tree)
Leetcode: https://leetcode.com/problems/implement-trie-prefix-tree/
1 | class Trie: |
14. Longest Common Prefix
Leetcode: https://leetcode.com/problems/longest-common-prefix/
1 | def longestCommonPrefix(self, strs: List[str]) -> str: |
212. Word Search II
Leetcode: https://leetcode.com/problems/word-search-ii/
421. Maximum XOR of Two Numbers in an Array
Leetcode: https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/