JIAKAOBO

LeetCode

venmo
wechat

感谢赞助!

  • ㊗️
  • 大家
  • offer
  • 多多!

Problem

Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.

Example: Assume that words = [“practice”, “makes”, “perfect”, “coding”, “makes”].

Input: word1 = “coding”, word2 = “practice”
Output: 3
Input: word1 = "makes", word2 = "coding"
Output: 1

Note: You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.

Code

class Solution {
    public int shortestDistance(String[] words, String word1, String word2) {
        int p1 = -1;
        int p2 = -1;

        int min = words.length;

        for(int i = 0; i < words.length; i++) {
            if(words[i].equals(word1)) {
                p1 = i;
            } else if (words[i].equals(word2)) {
                p2 = i;
            }

            if(p1 != -1 && p2 != -1) {
                min = Math.min(min, Math.abs(p1 - p2));
            }
        }

        return min;
    }
}
class Solution:
    def shortestDistance(self, words: List[str], word1: str, word2: str) -> int:
        m = -1
        n = -1

        d = len(words)

        for index, word in enumerate(words):
            if word == word1:
                m = index
            if word == word2:
                n = index

            if m != -1 and n != -1:
                d = min(d, abs(m - n))

        return d
class Solution:
    def shortestDistance(self, words: List[str], word1: str, word2: str) -> int:
        if not words or len(words) < 2:
            return -1

        map = {}
        d = len(words)

        for index, word in enumerate(words):
            if word == word1:
                if word2 in map:
                    d = min(d, index - map[word2])
            if word == word2:
                if word1 in map:
                    d = min(d, index - map[word1])

            map[word] = index

        return d