Python code with explanation


  • 0

    I suggest that you may write down the process on paper firstly for better understanding.

    class Solution(object):

    def shortestDistance(self, words, word1, word2):
        """
        :type words: List[str]
        :type word1: str
        :type word2: str
        :rtype: int
        """
        # current one (or you can call it previous one if you want) records the index and content so that we can compare with future ones
        c=None
        # d records the minimum length
        d=len(words)
        # iterate through words
        for i,x in enumerate(words):
            # find valid word and take action
            if (x == word1) or (x == word2):
                # if c exists, we can compare c with the found one
                if c:
                    # if the word of c is different from the found one
                    if x!=c[1]:
                        # it means we need to check if it is the minimum length
                        d=min(d,i-c[0])
                # anyway we need to update the current one since even the found word is the same as the current one, we still need to update it for the shortest distance
                c=(i,x)
        return d

Log in to reply
 

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