# Minimum Unique Word Abbreviation in C++

Suppose we have a string such as "word" and that contains the following abbreviations: ["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]. We also have a target string and a set of strings in a dictionary, we have to 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. Here each number or letter in the abbreviation is considered as length = 1. So as an example, the abbreviation "a32bc" is of length = 4.

So, if the input is like "apple", and dictionary is ["blade"], then the output will be "a4"

To solve this, we will follow these steps −

• Define an array dict

• Define a function abbrLen(), this will take mask,

• ret := n

• for initialize b := 3, when b < bn, update b <<= 1, do −

• if (mask AND b) is same as 0, then −

• (decrease ret by 1)

• return ret

• Define a function dfs(), this will take bit, mask,

• if len >= minLen, then −

• return

• match := true

• for each d in dict, do −

• if (mask AND d) is same as 0, then −

• match := false

• Come out from the loop

• if match is non-zero, then −

• minLen := len

• Otherwise −

• for initialize b := bit, when b < bn, update b := b*2 , do −

• if (cand AND b) is not equal to 0, then −

• From the main method do the following −

• ret := blank string

• n := size of target

• bn := 2^n

• cand := 0

• minLen := inf

• for each s in dictionary −

• if size of s is not equal to n, then −

• word := 0

• for initialize i := 0, when i < size of s, update (increase i by 1), do −

• if s[i] is not equal to target[i], then −

• word := word OR (2^i)

• insert word at the end of dict

• cand := cand OR word

• dfs(1, 0)

• for initialize i := 0, when i < n, do −

• if (minab AND 2^i) is not equal to 0, then −

• ret := ret + target[i]

• (increase i by 1)

• Otherwise

• j := i

• while (i < n and (minab AND 2^i) is same as 0), do −

• (increase i by 1)

• ret := ret concatenate (i - j)

• return ret

## Example

Let us see the following implementation to get a better understanding −

Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int n, cand, bn, minLen, minab;
vector<int> dict;
int ret = n;
for (int b = 3; b < bn; b <<= 1) {
if ((mask & b) == 0)
ret--;
}
return ret;
}
void dfs(int bit, int mask) {
if (len >= minLen)
return;
bool match = true;
for (int d : dict) {
if ((mask & d) == 0) {
match = false;
break;
}
}
if (match) {
minLen = len;
}
else {
for (int b = bit; b < bn; b <<= 1) {
if ((cand & b) != 0)
dfs(b << 1, mask | b);
}
}
}
string minAbbreviation(string target, vector<string> &dictionary) {
string ret = "";
n = target.size();
bn = 1 << n;
cand = 0;
minLen = INT_MAX;
for (string &s : dictionary) {
if (s.size() != n)
continue;
int word = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] != target[i])
word |= (1 << i);
}
dict.push_back(word);
cand |= word;
}
dfs(1, 0);
for (int i = 0; i < n;) {
if ((minab & (1 << i)) != 0) {
ret += target[i];
i++;
}
else {
int j = i;
while (i < n && (minab & (1 << i)) == 0)
i++;
ret += to_string(i - j);
}
}
return ret;
}
};
main() {
Solution ob;
cout << (ob.minAbbreviation("apple",v));
}

## Input

"apple",{"blade"}

## Output

a4