|
| 1 | +# 555. Split Concatenated Strings |
| 2 | + |
| 3 | +## #1 深度优先搜索[TLE] |
| 4 | + |
| 5 | +### 思路 |
| 6 | + |
| 7 | +通过深度优先遍历所有可能的组合,然后对每种可能的组合,遍历所有可能的切分点。 |
| 8 | + |
| 9 | +### 算法 |
| 10 | + |
| 11 | +```java |
| 12 | +class Solution { |
| 13 | + String res = ""; |
| 14 | + public String splitLoopedString(String[] strs) { |
| 15 | + dfs(strs, "", 0, strs.length); |
| 16 | + return res; |
| 17 | + } |
| 18 | + |
| 19 | + public void dfs(String[] strs, String s, int i, int n){ |
| 20 | + if(i < n){ |
| 21 | + dfs(strs, s + strs[i], i+1, n); |
| 22 | + dfs(strs, s + new StringBuffer(strs[i]).reverse().toString(), i+1, n); |
| 23 | + }else{ |
| 24 | + for(int j=0; j<s.length(); j++){ |
| 25 | + String cut = s.substring(j) + s.substring(0, j); |
| 26 | + if(cut.compareTo(res) > 0) |
| 27 | + res = cut; |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +### 复杂度分析 |
| 35 | + |
| 36 | +## #2 广度优先遍历[MLE] |
| 37 | + |
| 38 | +### 思路 |
| 39 | + |
| 40 | +想法和上面是一样的,就是在枚举的时候采用广度优先的做法。 |
| 41 | + |
| 42 | +### 算法 |
| 43 | + |
| 44 | +```java |
| 45 | +class Solution { |
| 46 | + public String splitLoopedString(String[] strs) { |
| 47 | + String res = ""; |
| 48 | + Queue<String> queue = new LinkedList<>(); |
| 49 | + queue.offer(""); |
| 50 | + int i=0, j=0; |
| 51 | + while(i < strs.length){ |
| 52 | + String cur = queue.poll(); |
| 53 | + queue.offer(cur+strs[i]); |
| 54 | + queue.offer(cur + new StringBuffer(strs[i]).reverse().toString()); |
| 55 | + j++; |
| 56 | + if(j == 1<<i){ |
| 57 | + i++; |
| 58 | + j=0; |
| 59 | + } |
| 60 | + } |
| 61 | + while(!queue.isEmpty()){ |
| 62 | + String cur = queue.poll(); |
| 63 | + for(int k=0; k<cur.length(); k++){ |
| 64 | + String cut = cur.substring(k) + cur.substring(0, k); |
| 65 | + if(cut.compareTo(res) > 0) |
| 66 | + res = cut; |
| 67 | + } |
| 68 | + } |
| 69 | + return res; |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +### 复杂度分析 |
| 75 | + |
| 76 | +## #3 优化枚举 |
| 77 | + |
| 78 | +### 思路 |
| 79 | + |
| 80 | +### 算法 |
| 81 | + |
| 82 | +```java |
| 83 | +class Solution { |
| 84 | + public String splitLoopedString(String[] strs) { |
| 85 | + for(int i=0; i<strs.length; i++){ |
| 86 | + String rev = new StringBuffer(strs[i]).reverse().toString(); |
| 87 | + if(rev.compareTo(strs[i]) > 0) |
| 88 | + strs[i] = rev; |
| 89 | + } |
| 90 | + String res = ""; |
| 91 | + for(int i=0; i<strs.length; i++){ |
| 92 | + String rev = new StringBuffer(strs[i]).reverse().toString(); |
| 93 | + for(String s : new String[]{strs[i], rev}){ |
| 94 | + for(int k=0; k<s.length(); k++){ |
| 95 | + StringBuffer temp = new StringBuffer(); |
| 96 | + temp.append(s.substring(k)); |
| 97 | + for(int j=i+1; j<strs.length; j++) |
| 98 | + temp.append(strs[j]); |
| 99 | + for(int j=0; j<i; j++){ |
| 100 | + temp.append(strs[j]); |
| 101 | + } |
| 102 | + temp.append(s.substring(0, k)); |
| 103 | + if(temp.toString().compareTo(res) > 0) |
| 104 | + res = temp.toString(); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + return res; |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
0 commit comments