This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?
Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list.
For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"]
.
Given word1 = “coding”
, word2 = “practice”
, return 3.
Given word1 = "makes"
, word2 = "coding"
, return 1.
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
根据题意,依旧是求两个词之间的最短距离。
class WordDistance(object):
def __init__(self, words):
"""
initialize your data structure here.
:type words: List[str]
"""
self.dic = {}
for index, value in enumerate(words): # key/value 对是 值/index索引list
if value not in self.dic:
self.dic[value] = [index]
else:
self.dic[value] += index,
def shortest(self, word1, word2):
"""
Adds a word into the data structure.
:type word1: str
:type word2: str
:rtype: int
"""
return min(abs(x-y) for x in self.dic[word1] for y in self.dic[word2])
class WordDistance(object):
def __init__(self, words):
"""
initialize your data structure here.
:type words: List[str]
"""
self.dic = {}
self.l = len(words)
for index, value in enumerate(words):
self.dic[value] = self.dic.get(value, []) + [index] # dictionary.get(),可以传入一个参数。如果传入两个参数,第二个参数就是default值,如果字典中找不到的话,就返回default值。
def shortest(self, word1, word2):
"""
Adds a word into the data structure.
:type word1: str
:type word2: str
:rtype: int
"""
l1, l2 = self.dic[word1], self.dic[word2]
i = j = 0
res = self.l
# O(m+n) time complexity
while i < len(l1) and j < len(l2):
res = min(res, abs(l1[i]-l2[j])) # loop all the l1 and l2 to find the shortest
if l1[i] < l2[j]:
i += 1
else:
j += 1
return res