forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFindTheShortestSuperstring.java
More file actions
72 lines (58 loc) · 2.12 KB
/
FindTheShortestSuperstring.java
File metadata and controls
72 lines (58 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class Solution {
public String shortestSuperstring(String[] words) {
Map<String, String> map = new HashMap<>();
// mark every word as unused
int unused = 0; // integer is used as a bit array
for(int i = 0; i < words.length; i++) {
unused |= 1 << i;
}
return shortestSuperstring(words, "", unused, map);
}
private String shortestSuperstring(String[] words, String startWord, int unused, Map<String, String> map) {
if (unused == 0) {
return startWord;
}
// check
String key = startWord + "|" + unused;
if (map.containsKey(key)) {
return map.get(key);
}
String shortest = null;
for(int i = 0; i < words.length; i++) {
if (!isConsumed(unused, i)) {
// get the shortest superstring starting from an unused word
String superstring = shortestSuperstring(words, words[i], consume(unused, i), map);
// "append" the shortest superstring to the start word
String appended = overlapAppend(startWord, superstring);
// keep the shortest
if (shortest == null || appended.length() < shortest.length()) {
shortest = appended;
}
}
}
map.put(key, shortest);
return shortest;
}
// Concat string a to b . For example, "bake" and "kelt" => "bakelt"
private String overlapAppend(String a, String b) {
for(int i = Math.max(1, a.length() - b.length()); i < a.length(); i++) {
boolean match = true;
for(int j = i; j < a.length(); j++) {
if (a.charAt(j) != b.charAt(j - i)) {
match = false;
break;
}
}
if (match) {
return a.substring(0, i) + b;
}
}
return a + b;
}
private boolean isConsumed(int unused, int i) {
return ((unused >> i) & 1) == 0;
}
private int consume(int unused, int i) {
return unused & ~(1 << i);
}
}