Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Brute force Solution

Thai Minh

Jul 30, 2023

class Solution: def shortestDistance(self, words, word1, word2): # TODO: Write your code here # Find index of word1 and word 2 in words array # when we update the indexes reupdate the min distance between index1 and index2 index1, index2 = -1, -1 curr_min = len(words) for i, word in enumerate(words): if word == word1: index1 = i if word == word2: index2 = i if index1 != -1 and index2 != -1: curr_min = min(curr_min, abs(index1 - index2)) return curr_min

1

0

Comments
Comments