December_week_1_bonus

Leetcode December Challenge Day 1 Link to heading

Link to the question

Solution

class Solution(object):
    def shortestDistance(self, words, word1, word2):
        """
        :type words: List[str]
        :type word1: str
        :type word2: str
        :rtype: int
        """
        val1 = val2 = None
        dist = len(words)
        for i in range(len(words)):
            if words[i] == word1:
                val1 = i
            if words[i] == word2:
                val2 = i
            if val1 != None and val2 != None:
                dist = min(dist,abs(val1-val2)) 
        return dist