xamples
epeat
ode
pproach
ptimize
est
Given
find and return that word's definition (if it exists).
This array of dictionary entries will be formatted like so:
insert 'a'
[ 'a' ]
Entries are alphabetically sorted!
(aka Brute Force)
insert 'a'
[ 'a' ]
(aka Brute Force)
insert 'a'
[ 'a' ]
insert 'a'
[ 'a' ]
What have we not taken full advantage of yet?
Binary search
insert 'a'
[ 'a' ]
What next?
Binary search
insert 'a'
[ 'a' ]
Binary search
insert 'a'
[ 'a' ]
Strings compared by using > and <
O(m * log n) time
n = dict.length
m = word.length
O(1) space
insert 'a'
[ 'a' ]
We usually use dictionaries more than once-
can we optimize for this use case?
insert 'a'
[ 'a' ]
First run: O(n) time to build dictionary object
Subsequent runs: O(1) lookup time!
O(n) space to store our dictionary object
insert 'a'
[ 'a' ]
insert 'a'
[ 'a' ]
Approach | Time complexity | Space complexity |
---|---|---|
Naive | O(n * m) | O(1)*** |
Binary Search | O(m * log n) | O(1)*** |
Hash Map | O(1) (first time O(n)) | O(n) |
What else did you learn from this problem?
What questions do you have?
n = dict.length, m = word.length
*** Might also be considered O(e), entry.length