## 527. Word Abbreviation

Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations for every word following rules below.

1. Begin with the first character and then the number of characters abbreviated, which followed by the last character.
2. If there are any conflict, that is more than one words share the same abbreviation, a longer prefix is used instead of only the first character until making the map from word to abbreviation become unique. In other words, a final abbreviation cannot map to more than one original words.
3. If the abbreviation doesn't make the word shorter, then keep it as original.

Example:

Input: ["like", "god", "internal", "me", "internet", "interval", "intension", "face", "intrusion"]
Output: ["l2e","god","internal","me","i6t","interval","inte4n","f2e","intr4n"]

Note:
1. Both n and the length of each word will not exceed 400.
2. The length of each word is greater than 1.
3. The words consist of lowercase English letters only.
4. The return answers should be in the same order as the original array.

## Rust Solution

struct Solution;

use std::collections::HashMap;

impl Solution {
fn words_abbreviation(dict: Vec<String>) -> Vec<String> {
let mut count: HashMap<(&str, &str, usize), usize> = HashMap::new();
for s in &dict {
let n = s.len();
let suffix = &s[n - 1..];
for i in 1..n {
let prefix = &s[..i];
let k = n - i - 1;
*count.entry((prefix, suffix, k)).or_default() += 1;
}
}
let mut res = vec![];
for s in &dict {
let n = s.len();
let suffix = &s[n - 1..];
for i in 1..n {
let prefix = &s[..i];
let k = n - i - 1;
if let Some(1) = count.get(&(prefix, suffix, k)) {
if k <= 1 {
res.push(s.to_string());
} else {
res.push(format!("{}{}{}", prefix, k, suffix));
}
break;
}
}
}
res
}
}

#[test]
fn test() {
let dict = vec_string![
"like",
"god",
"internal",
"me",
"internet",
"interval",
"intension",
"face",
"intrusion"
];
let res =
vec_string!["l2e", "god", "internal", "me", "i6t", "interval", "inte4n", "f2e", "intr4n"];
assert_eq!(Solution::words_abbreviation(dict), res);
}

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