forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_555.java
44 lines (41 loc) · 1.65 KB
/
_555.java
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
package com.fishercoder.solutions;
public class _555 {
public static class Solution1 {
/**
* credit: https://discuss.leetcode.com/topic/86477/neat-java-solution
* and article: https://leetcode.com/articles/split-assembled-strings/#approach-3-optimized-solution-accepted
*/
public String splitLoopedString(String[] strs) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < strs.length; i++) {
sb.setLength(0);
String reverse = sb.append(strs[i]).reverse().toString();
if (strs[i].compareTo(reverse) < 0) {
strs[i] = reverse;
}
}
String result = "";
for (int i = 0; i < strs.length; i++) {
sb.setLength(0);
String reverse = sb.append(strs[i]).reverse().toString();
for (String str : new String[]{strs[i], reverse}) {
for (int k = 0; k < str.length(); k++) {
sb.setLength(0);
sb.append(str.substring(k));
for (int j = i + 1; j < strs.length; j++) {
sb.append(strs[j]);
}
for (int j = 0; j < i; j++) {
sb.append(strs[j]);
}
sb.append(str.substring(0, k));
if (sb.toString().compareTo(result) > 0) {
result = sb.toString();
}
}
}
}
return result;
}
}
}