old_contents: # Time:  O(n)
  # Space: O(1)
  
  class Solution:
      # @param {string[]} words
      # @param {string} word1
      # @param {string} word2
      # @return {integer}
      def shortestWordDistance(self, words, word1, word2):
          dist = float("inf")
          i, index1, index2 = 0, None, None
          while i < len(words):
              if words[i] == word1:
                  if index1 is not None:
                      dist = min(dist, abs(index1 - i))
                  index1 = i
              elif words[i] == word2:
                  index2 = i
  
              if index1 is not None and index2 is not None:
                  dist = min(dist, abs(index1 - index2))
              i += 1
  
          return dist
  
new_contents: # Time:  O(n)
  # Space: O(1)
  
  class Solution:
      # @param {string[]} words
      # @param {string} word1
      # @param {string} word2
      # @return {integer}
      def shortestWordDistance(self, words, word1, word2):
          dist = float("inf")
          is_same = (word1 == word2)
          i, index1, index2 = 0, None, None
          while i < len(words):
              if words[i] == word1:
                  if is_same and index1 is not None:
                      dist = min(dist, abs(index1 - i))
                  index1 = i
              elif words[i] == word2:
                  index2 = i
  
              if index1 is not None and index2 is not None:
                  dist = min(dist, abs(index1 - index2))
              i += 1
  
          return dist
  
subject: Update shortest-word-distance-iii.py