My Python Solution

  • 0
    class WordDistance(object):
        def __init__(self, words):
            :type words: List[str]
            # save words and indexes into map
            self.table = {}
            for index, word in enumerate(words):
                if word not in self.table:
                    self.table[word] = [index]
        def shortest(self, word1, word2):
            :type word1: str
            :type word2: str
            :rtype: int
            return min([abs(el1-el2) for el1 in self.table[word1] for el2 in self.table[word2]])

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.