411 Minimum Unique Word Abbreviation

1. Question

A string such as"word"contains the following abbreviations:
1
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]
Copied!
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.
Examples:
1
"apple", ["blade"] -> "a4" (because "5" or "4e" conflicts with "blade")
2
3
"apple", ["plain", "amber", "blade"] -> "1p3" (other valid answers include "ap3", "a3e", "2p2", "3le", "3l1").
Copied!

2. Implementation

(1) Backtracking
思路: 这是320和408两道题的结合体,做法是对target,我们利用320的方法,生成所有可能的abbreviations,将这些abbreviations放在一个按照长度从小到大排序的minHeap里。然后每次从minHeap取出一个abbreviation,里用408的方法查看dictionary里是否有word的abbreviation和它一样,如果有则说明当前的abbreviation有冲突,直接跳出循环,查看下一个长度的abbreviation
1
class Solution {
2
public String minAbbreviation(String target, String[] dictionary) {
3
if (dictionary == null || dictionary.length == 0) {
4
return String.valueOf(target.length());
5
}
6
7
PriorityQueue<String> abbreviations = new PriorityQueue<>((a, b)->(a.length() - b.length()));
8
getAbbreviations(target, 0, 0, "", abbreviations);
9
10
while (!abbreviations.isEmpty()) {
11
String curAbbreviation = abbreviations.remove();
12
boolean noConflict = true;
13
14
for (String word : dictionary) {
15
if (isValidAbbreviation(word, curAbbreviation)) {
16
noConflict = false;
17
break;
18
}
19
}
20
21
if (noConflict) {
22
return curAbbreviation;
23
}
24
}
25
return "";
26
}
27
28
public void getAbbreviations(String target, int index, int count, String abbr, PriorityQueue<String> abbreviations) {
29
if (index == target.length()) {
30
if (count > 0) {
31
abbr += count;
32
}
33
abbreviations.add(abbr);
34
return;
35
}
36
37
getAbbreviations(target, index + 1, count + 1, abbr, abbreviations);
38
getAbbreviations(target, index + 1, 0, abbr + (count == 0 ? "" : count) + target.charAt(index), abbreviations);
39
}
40
41
public boolean isValidAbbreviation(String word, String abbr) {
42
int i = 0, j = 0;
43
int num = 0;
44
45
while (i < word.length() && j < abbr.length()) {
46
if (Character.isDigit(abbr.charAt(j))) {
47
num = 10 * num + abbr.charAt(j) - '0';
48
49
if (num == 0) {
50
return false;
51
}
52
++j;
53
}
54
else {
55
i += num;
56
57
if (i >= word.length() || word.charAt(i) != abbr.charAt(j)) {
58
return false;
59
}
60
num = 0;
61
++i;
62
++j;
63
}
64
}
65
i += num;
66
67
return i == word.length() && j == abbr.length();
68
}
69
}
Copied!

3. Time & Space Complexity

Backtracking: 时间复杂度O(2^n + n * 2^n + m * n * 2^n))
解析: n是target的长度, m是dictionary里word个数,时间复杂度可以分成3个部分,第一部分是getAbbreviations(), 需要O(2^n)。第二部分是while loop,我们从minHeap取出abbreviation, 每次取abbreviation需要log(2^n)的时间,总共有2^n次,所以这部分需要时间 O(n * 2^n)。第三部分是while loop里, 对每个abbreviation, 我们对dictionary里每个word都call一次isValidAbbreviation(), dictionary有m个word,堆里总共有2^n abbreviation, 总共需要call m * 2^n次, isValidAbbreviation需要O(n), 这部分需要O(m * n * 2^n)
空间复杂度O(n + 2^n), 递归深度为n,堆总共有2^n个abbreviation