Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public int shortestDistance(String[] wordsDict, String word1, String word2) {
- Map<String, List<Integer>> map = new HashMap<>();
- for(int i = 0; i < wordsDict.length; i++) {
- List<Integer> indexList = map.getOrDefault(wordsDict[i], new ArrayList<>());
- indexList.add(i);
- map.put(wordsDict[i], indexList);
- }
- int min = Integer.MAX_VALUE;
- for(int i : map.get(word1)) {
- for(int j : map.get(word2)) {
- min = Math.min(Math.abs(i-j), min);
- }
- }
- return min;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement