-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
41 lines (41 loc) · 1.32 KB
/
Solution.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
class Solution {
public String splitLoopedString(String[] strs) {
int n = strs.length;
for (int i = 0; i < n; ++i) {
String s = strs[i];
String t = new StringBuilder(s).reverse().toString();
if (s.compareTo(t) < 0) {
strs[i] = t;
}
}
String ans = "";
for (int i = 0; i < n; ++i) {
String s = strs[i];
StringBuilder sb = new StringBuilder();
for (int j = i + 1; j < n; ++j) {
sb.append(strs[j]);
}
for (int j = 0; j < i; ++j) {
sb.append(strs[j]);
}
String t = sb.toString();
for (int j = 0; j < s.length(); ++j) {
String a = s.substring(j);
String b = s.substring(0, j);
String cur = a + t + b;
if (ans.compareTo(cur) < 0) {
ans = cur;
}
cur = new StringBuilder(b)
.reverse()
.append(t)
.append(new StringBuilder(a).reverse().toString())
.toString();
if (ans.compareTo(cur) < 0) {
ans = cur;
}
}
}
return ans;
}
}