411. Minimum Unique Word Abbreviation

A string such as "word" contains the following abbreviations:

["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]

Given a target string and a set of strings in a dictionary, find an abbreviation of this target string with the smallest possible length such that it does not conflict with abbreviations of the strings in the dictionary.

Each number or letter in the abbreviation is considered length = 1. For example, the abbreviation "a32bc" has length = 4.

Examples:

"apple", ["blade"] -> "a4" (because "5" or "4e" conflicts with "blade")

"apple", ["plain", "amber", "blade"] -> "1p3" (other valid answers include "ap3", "a3e", "2p2", "3le", "3l1").

 

Constraints:

  • In the case of multiple answers as shown in the second example below, you may return any one of them.
  • Assume length of target string = m, and dictionary size = n. You may assume that m ≤ 21, n ≤ 1000, and log2(n) + m ≤ 20.

Rust Solution

struct Solution;

impl Solution {
    fn min_abbreviation(target: String, dictionary: Vec<String>) -> String {
        let n = target.len();
        let target: Vec<char> = target.chars().collect();
        let dictionary: Vec<Vec<char>> = dictionary
            .into_iter()
            .filter(|s| s.len() == n)
            .map(|s| s.chars().collect())
            .collect();
        let mut mask: u32 = 0;
        let mut res: u32 = !0;
        Self::dfs(0, &mut mask, &mut res, &target, &dictionary, n);
        Self::abbreviate(&target, res, n)
    }

    fn dfs(
        start: usize,
        mask: &mut u32,
        res: &mut u32,
        target: &[char],
        dictionary: &[Vec<char>],
        n: usize,
    ) {
        if start == n {
            for word in dictionary.iter() {
                if Self::conflict(target, word, *mask, n) {
                    return;
                }
            }
            if Self::length(*mask, n) < Self::length(*res, n) {
                *res = *mask;
            }
        } else {
            Self::dfs(start + 1, mask, res, target, dictionary, n);
            *mask |= 1 << start;
            Self::dfs(start + 1, mask, res, target, dictionary, n);
            *mask &= !(1 << start);
        }
    }

    fn conflict(target: &[char], word: &[char], mask: u32, n: usize) -> bool {
        (0..n).all(|i| mask & 1 << i == 0 || target[i] == word[i])
    }

    fn abbreviate(target: &[char], mask: u32, n: usize) -> String {
        let mut i = 0;
        let mut res = "".to_string();
        while i < n {
            if mask & 1 << i == 0 {
                let mut size = 1;
                while i + 1 < n && mask & 1 << (i + 1) == 0 {
                    size += 1;
                    i += 1;
                }
                res.push_str(&size.to_string());
            } else {
                res.push(target[i]);
            }
            i += 1;
        }
        res
    }

    fn length(mask: u32, n: usize) -> usize {
        let mut i = 0;
        let mut res = 0;
        while i < n {
            if mask & 1 << i == 0 {
                while i + 1 < n && mask & 1 << (i + 1) == 0 {
                    i += 1;
                }
            }
            res += 1;
            i += 1;
        }
        res
    }
}

#[test]
fn test() {
    let target = "apple".to_string();
    let dictionary = vec_string!["blade"];
    let res = "a4".to_string();
    assert_eq!(Solution::min_abbreviation(target, dictionary), res);
    let target = "apple".to_string();
    let dictionary = vec_string!["plain", "amber", "blade"];
    let res = "3l1".to_string();
    assert_eq!(Solution::min_abbreviation(target, dictionary), res);
}

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