|
| 1 | +## 943. Find the Shortest Superstring |
| 2 | + |
| 3 | +### Question: |
| 4 | +Given an array A of strings, find any smallest string that contains each string in A as a substring. |
| 5 | +We may assume that no string in A is substring of another string in A. |
| 6 | + |
| 7 | +``` |
| 8 | +Example 1: |
| 9 | +
|
| 10 | +Input: ["alex","loves","leetcode"] |
| 11 | +Output: "alexlovesleetcode" |
| 12 | +Explanation: All permutations of "alex","loves","leetcode" would also be accepted. |
| 13 | +
|
| 14 | +Example 2: |
| 15 | +
|
| 16 | +Input: ["catg","ctaagt","gcta","ttca","atgcatc"] |
| 17 | +Output: "gctaagttcatgcatc" |
| 18 | +``` |
| 19 | + |
| 20 | +Note: |
| 21 | +* 1 <= A.length <= 12 |
| 22 | +* 1 <= A[i].length <= 20 |
| 23 | + |
| 24 | + |
| 25 | +### Solution: |
| 26 | +* Method 1: search O(N! + N^2) AC 775ms |
| 27 | + * First create a cost[i][j] matrix, means the number of characters need to be added at the end of the result if last added word is A[i] and we want to add A[j]. Cost matrix is calculated at the very beginning so it will be calculated once, and its time efficiency is O(n^2). |
| 28 | + * We list all possible permutations and save the minimum. Pruning is required for remove useless iterations. |
| 29 | + * We only record the path of words it goes through and concat the string at the end(we only concat once). |
| 30 | + ```Java |
| 31 | + class Solution { |
| 32 | + public String shortestSuperstring(String[] A) { |
| 33 | + if(A.length == 1) return A[0]; |
| 34 | + int[][] cost = new int[A.length][A.length]; |
| 35 | + for(int i = 0; i < A.length; i++){ |
| 36 | + LABEL: |
| 37 | + for(int j = 0; j < A.length; j++){ |
| 38 | + if(i == j){ |
| 39 | + cost[i][j] = A[j].length(); |
| 40 | + }else{ |
| 41 | + int start = A[i].indexOf(A[j].charAt(0)); |
| 42 | + if(start == -1) cost[i][j] = A[j].length(); |
| 43 | + else{ |
| 44 | + for(int k = start; k < A[i].length(); k++){ |
| 45 | + if(A[j].startsWith(A[i].substring(k))){ |
| 46 | + cost[i][j] = A[j].length() - (A[i].length() - k); |
| 47 | + continue LABEL; |
| 48 | + } |
| 49 | + } |
| 50 | + cost[i][j] = A[j].length(); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + List<Integer> res = new ArrayList<>(); |
| 56 | + dfs(A, cost, 0, 0, new ArrayList<Integer>(), res, 0); |
| 57 | + StringBuilder sb = new StringBuilder(); |
| 58 | + for(int i = 0; i < res.size(); i++){ |
| 59 | + Integer path = res.get(i); |
| 60 | + sb.append(i == 0 ? A[path]: A[path].substring(A[path].length() - cost[res.get(i - 1)][path])); |
| 61 | + } |
| 62 | + return sb.toString(); |
| 63 | + } |
| 64 | + private int sum = Integer.MAX_VALUE; |
| 65 | + private void dfs(String[] A, int[][] cost, int index, int temp, List<Integer> path, List<Integer> res, int visited){ |
| 66 | + if(temp >= sum) return; |
| 67 | + if(A.length == index){ |
| 68 | + sum = temp; |
| 69 | + res.clear(); |
| 70 | + res.addAll(path); |
| 71 | + return; |
| 72 | + } |
| 73 | + for(int i = 0; i < A.length; i++){ |
| 74 | + if((visited & (1 << i)) > 0) continue; |
| 75 | + path.add(i); |
| 76 | + dfs(A, |
| 77 | + cost, |
| 78 | + index + 1, |
| 79 | + index == 0 ? A[i].length(): temp + cost[path.get(index - 1)][i], |
| 80 | + path, |
| 81 | + res, |
| 82 | + (visited | (1 << i))); |
| 83 | + path.remove(index); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + ``` |
| 88 | + |
| 89 | +* Method 2: DP Need to fill when doing the dp questions. |
| 90 | + |
| 91 | +### Reference |
| 92 | +1. [花花酱 LeetCode 943. Find the Shortest Superstring - 刷题找工作 EP231](https://www.youtube.com/watch?v=u_Wc4jwrp3Q) |
| 93 | + |
0 commit comments