243. Shortest Word Distance

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.

Rust Solution

struct Solution;

use std::collections::HashMap;
use std::i32;

impl Solution {
    fn shortest_distance(words: Vec<String>, word1: String, word2: String) -> i32 {
        let mut pos: HashMap<String, Vec<i32>> = HashMap::new();
        let mut min = i32::MAX;
        for i in 0..words.len() {
            let w = &words[i];
            if let Some(v) = pos.get_mut(w) {
                v.push(i as i32);
            } else {
                pos.insert(w.clone(), vec![i as i32]);
            }
        }
        let v1 = pos.get(&word1).unwrap();
        let v2 = pos.get(&word2).unwrap();
        for &i in v1 {
            for &j in v2 {
                min = i32::min(min, (i - j).abs());
            }
        }
        min
    }
}

#[test]
fn test() {
    let words: Vec<String> = vec_string!["practice", "makes", "perfect", "coding", "makes"];
    let word1 = "coding".to_string();
    let word2 = "practice".to_string();
    assert_eq!(Solution::shortest_distance(words, word1, word2), 3);
    let words: Vec<String> = vec_string!["practice", "makes", "perfect", "coding", "makes"];
    let word1 = "makes".to_string();
    let word2 = "coding".to_string();
    assert_eq!(Solution::shortest_distance(words, word1, word2), 1);
}

Having problems with this solution? Click here to submit an issue on github.