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.
Note:
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.
We want the shortest abbreviation of the target that does not conflict with any abbreviation of the dictionary words. We can use bitmasks to represent which positions are kept as letters and which are abbreviated as numbers, and check for conflicts efficiently.
For each word in the dictionary with the same length as the target, compute a bitmask of positions where the word differs from the target.
For all possible abbreviation masks, check if the abbreviation is unique (i.e., for every mask, there is at least one differing position with every dictionary word).
For each valid mask, compute the abbreviation length.
classSolution {
public: string minAbbreviation(string target, vector<string>& dictionary) {
int m = target.size();
vector<int> diffs;
for (auto& word : dictionary) {
if (word.size() != m) continue;
int diff =0;
for (int i =0; i < m; ++i) {
if (word[i] != target[i]) diff |= (1<< i);
}
diffs.push_back(diff);
}
int min_len = m +1, res_mask =0;
for (int mask =0; mask < (1<< m); ++mask) {
bool valid = true;
for (int d : diffs) {
if ((mask & d) ==0) { valid = false; break; }
}
if (valid) {
int len =0, cnt =0;
for (int i =0; i < m; ++i) {
if (mask & (1<< i)) {
if (cnt) { ++len; cnt =0; }
++len;
} else {
++cnt;
if (i == m-1|| (mask & (1<< (i+1)))) { ++len; cnt =0; }
}
}
if (len < min_len) { min_len = len; res_mask = mask; }
}
}
string ans;
int cnt =0;
for (int i =0; i < m; ++i) {
if (res_mask & (1<< i)) {
if (cnt) { ans += to_string(cnt); cnt =0; }
ans += target[i];
} else {
++cnt;
if (i == m-1|| (res_mask & (1<< (i+1)))) { ans += to_string(cnt); cnt =0; }
}
}
return ans;
}
};
classSolution {
funminAbbreviation(target: String, dictionary: Array<String>): String {
val m = target.length
val diffs = mutableListOf<Int>()
for (word in dictionary) {
if (word.length != m) continuevar diff = 0for (i in0 until m) {
if (word[i] != target[i]) diff = diff or (1 shl i)
}
diffs.add(diff)
}
var minLen = m + 1var resMask = 0for (mask in0 until (1 shl m)) {
var valid = truefor (d in diffs) {
if (mask and d ==0) { valid = false; break }
}
if (valid) {
var len = 0var cnt = 0for (i in0 until m) {
if (mask and (1 shl i) !=0) {
if (cnt > 0) { len++; cnt = 0 }
len++ } else {
cnt++if (i == m-1|| mask and (1 shl (i+1)) !=0) { len++; cnt = 0 }
}
}
if (len < minLen) { minLen = len; resMask = mask }
}
}
val ans = StringBuilder()
var cnt = 0for (i in0 until m) {
if (resMask and (1 shl i) !=0) {
if (cnt > 0) { ans.append(cnt); cnt = 0 }
ans.append(target[i])
} else {
cnt++if (i == m-1|| resMask and (1 shl (i+1)) !=0) { ans.append(cnt); cnt = 0 }
}
}
return ans.toString()
}
}
from typing import List
classSolution:
defminAbbreviation(self, target: str, dictionary: List[str]) -> str:
m = len(target)
diffs = []
for word in dictionary:
if len(word) != m:
continue diff =0for i in range(m):
if word[i] != target[i]:
diff |= (1<< i)
diffs.append(diff)
min_len, res_mask = m +1, 0for mask in range(1<< m):
valid =Truefor d in diffs:
if (mask & d) ==0:
valid =Falsebreakif valid:
length, cnt =0, 0for i in range(m):
if mask & (1<< i):
if cnt:
length +=1 cnt =0 length +=1else:
cnt +=1if i == m -1or mask & (1<< (i +1)):
length +=1 cnt =0if length < min_len:
min_len = length
res_mask = mask
ans ='' cnt =0for i in range(m):
if res_mask & (1<< i):
if cnt:
ans += str(cnt)
cnt =0 ans += target[i]
else:
cnt +=1if i == m -1or res_mask & (1<< (i +1)):
ans += str(cnt)
cnt =0return ans