LeetCode 1048. Longest String Chain

Problem Statement


use std::collections::HashMap;

impl Solution {
pub fn longest_str_chain(words: Vec<String>) -> i32 {
let (mut ans, mut w) = (0, words);
w.sort_by_key(|x| x.len());
let mut map = HashMap::new();

for word in w {
if map.contains_key(&word) {
continue;
}
map.insert(word.clone(), 1);
for i in 0..word.len() {
let mut c = word.clone();
c.remove(i);
if map.contains_key(&c) &&
(*map.get(&c).unwrap() + 1 > *map.get(&word).unwrap()) {
map.insert(word.clone(), map.get(&c).unwrap() + 1);
}
}
ans = std::cmp::max(ans, *map.get(&word).unwrap());
}
return ans;
}
}