243. Shortest Word Distance

Easy (LinkedIn)

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

For example, Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

Given word1 = “coding”, word2 = “practice”, return 3. Given word1 = "makes", word2 = "coding", return 1.

Note:

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

public class Solution {

// test case:
// words is empty or word1 is null or word2 is null
// wether the word1 and word2 are both in the array
// word1 is equals to word2

public int shortestDistance(String[] words, String word1, String word2) {
    if(words == null || words.length == 0 || word1 == null || word2 == null) {
        return 0;
    }

    int len = words.length;
    Map<String, Integer> map = new HashMap<>();
    int minDistance = Integer.MAX_VALUE;

    for(int i = 0; i < len; i++) {
        if(words[i].equals(word1)) {
            if(map.containsKey(word2)) {
                minDistance = Math.min(minDistance, i - map.get(word2));
            }
        } else if(words[i].equals(word2)) {
            if(map.containsKey(word1)) {
                minDistance = Math.min(minDistance, i - map.get(word1));
            }
        } 

        map.put(words[i], i);
    }

    return minDistance;
}

}

Follow up:

If we want to get the longest distance between word1 and word2, we just need to record the first index and last index of word1 and word2, and then calculate the max distance.


244. Shortest Word Distance II

Medium (LinkedIn)

This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?

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.

For example,

Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

Given word1 = “coding”, word2 = “practice”, return 3. Given word1 = "makes", word2 = "coding", return 1.

Note:

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

public class WordDistance {

private Map<String, List<Integer>> map;


public WordDistance(String[] words) {
    if(words == null || words.length == 0) {
        return ;
    }

    map = new HashMap<String, List<Integer>>();

    for(int i = 0; i < words.length; i++) {
        if(map.containsKey(words[i])) {
            map.get(words[i]).add(i);
        } else {
            List<Integer> list = new ArrayList<>();
            list.add(i);
            map.put(words[i], list);
        }
    }
}

public int shortest(String word1, String word2) {
    if(!map.containsKey(word1) || !map.containsKey(word2)) {
        return 0;
    }

    List<Integer> list1 = map.get(word1);
    List<Integer> list2 = map.get(word2);
    int index1 = 0, index2 = 0;
    int minDistance = Integer.MAX_VALUE;

    while(index1 < list1.size() && index2 < list2.size()) {
        int num1 = list1.get(index1);
        int num2 = list2.get(index2);
        minDistance = Math.min(minDistance, Math.abs(num1 - num2));

        if(num1 < num2) {
            index1++;
        } else {
            index2++;
        }
    }

    return minDistance;
}

}


245. Shortest Word Distance III

Medium (LinkedIn)

This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2.

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

word1 and word2 may be the same and they represent two individual words in the list.

For example,

Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

Given word1 = “makes”, word2 = “coding”, return 1. Given word1 = "makes", word2 = "makes", return 3.

Note:

You may assume word1 and word2 are both in the list.

Explaination:

Classified discussion:

  1. word1 is not equal to word2
  2. word2 is equals to word1

public class Solution {

public int shortestWordDistance(String[] words, String word1, String word2) {
    if(words == null || words.length == 0 || word1 == null || word2 == null) {
        return 0;
    }

    int minDistance = Integer.MAX_VALUE;

    if(!word1.equals(word2)) {
        Map<String, Integer> map = new HashMap<>();

        for(int i = 0; i < words.length; i++) {
            if(words[i].equals(word1)) {
                if(map.containsKey(word2)) {
                    minDistance = Math.min(minDistance, i - map.get(word2));
                }
            } else if(words[i].equals(word2)) {
                if(map.containsKey(word1)) {
                    minDistance = Math.min(minDistance, i - map.get(word1));
                }
            } 

            map.put(words[i], i);
        }
    } else {
        int prevPos = -1;

        for(int i = 0; i < words.length; i++) {
            if(words[i].equals(word1)) {
                if(prevPos != -1) {
                    minDistance = Math.min(minDistance, i - prevPos);
                }

                prevPos = i;
            }
        }
    }

    return minDistance;
}

}

results matching ""

    No results matching ""