JIAKAOBO

LeetCode

venmo
wechat

感谢赞助!

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

Problem

Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list. Your method will be called repeatedly many times with different parameters.

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 WordDistance {
    HashMap<String, List<Integer>> map;
    public WordDistance(String[] words) {
        this.map = new HashMap<>();
        for(int i = 0; i < words.length; i++) {
            if(!map.containsKey(words[i])) {
                map.put(words[i], new ArrayList<>());
            }

            map.get(words[i]).add(i);
        }
    }

    public int shortest(String word1, String word2) {
        List<Integer> l1 = map.get(word1);
        List<Integer> l2 = map.get(word2);

        int p1 = 0;
        int p2 = 0;

        int min = Integer.MAX_VALUE;
        while (p1 < l1.size() && p2 < l2.size()) {
            min = Math.min(min, Math.abs(l1.get(p1) - l2.get(p2)));
            if(l1.get(p1) < l2.get(p2)) {
                p1++;
            } else {
                p2++;
            }
        }

        return min;
    }
}
class WordDistance {
    HashMap<String, List<Integer>> map;
    public WordDistance(String[] words) {
        this.map = new HashMap<>();
        for(int i = 0; i < words.length; i++) {
            if(!map.containsKey(words[i])) {
                map.put(words[i], new ArrayList<>());
            }

            map.get(words[i]).add(i);
        }
    }

    public int shortest(String word1, String word2) {
        List<Integer> l1 = map.get(word1);
        List<Integer> l2 = map.get(word2);

        int min = Integer.MAX_VALUE;
        for(int n1 : l1) {
            for(int n2 : l2) {
                min = Math.min(min, Math.abs(n1 - n2));
            }
        }

        return min;
    }
}
class WordDistance:

    def __init__(self, words: List[str]):
        self.map = defaultdict(list)
        for index, word in enumerate(words):
            self.map[word].append(index)


    def shortest(self, word1: str, word2: str) -> int:
        l1 = self.map[word1]
        l2 = self.map[word2]

        i = 0
        j = 0

        res = float("inf")

        while i < len(l1) and j < len(l2):
            res = min(res, abs(l1[i] - l2[j]))
            if l1[i] < l2[j]:
                i += 1
            else:
                j += 1

        return res
class WordDistance:

    def __init__(self, words: List[str]):
        self.map = defaultdict(list)
        for index, word in enumerate(words):
            self.map[word].append(index)


    def shortest(self, word1: str, word2: str) -> int:
        l1 = self.map[word1]
        l2 = self.map[word2]

        res = float("inf")

        for a in l1:
            for b in l2:
                res = min(res, abs(a - b))

        return res